Axel Magnuson
Axel Magnuson

Reputation: 1202

Node express: 404 on some routes but not others

So I have a problem where some of my express.js (v3.2.0) routes are returning a 404 status code, while others are not. I can't figure out why some routes are working fine while other ones aren't resolving. Any help would be greatly appreciated.

server.js:

//npm packages
var express = require('express')
  , app = express()
  , server = require('http').createServer(app)
  ;
if(process.env.NODE_ENV == 'production') require('newrelic');

//local resources
init = require('./lib/init');
ctrl = require('./lib/ctrl')

//external async resources (global variables)
init.setup(function(){

  //configure express
  _util.configExpress(__dirname, app, express);

  // create a new AI classifier
  //
  // Arguments
  // ---------
  //    modelType   - (path param) ID of the classifier
  //    modelParams - (body) JSON containing to configure the model
  //
  // Returns
  // -------
  //    200 - ID of created classifier
  //    400 - Error during creation
  app.get('/clf/create/:modelType', _util.auth(express.basicAuth), function(req, res) {
    var modelParams = req.body ? req.body : {};
    var modelType = req.params.modelType;
    ctrl.clfCreate(modelType, modelParams, function(err, result) {
      if(err) {
        // at this point, assume bad request
        res.status(400);
        res.json({"err": err.message});
      } else {
        // if success, return classifier ID
        res.json({"id": result});
      }
      res.end();
    });
  });

  // [Unrelated endpoints...]

  //handle resource update data from io server
  app.all('/:clientId/:projectId/train', _util.auth(express.basicAuth), function (req, res){
    ctrl.trainer(req.params.clientId, req.params.projectId, function(err, result){/*res.json(result);*/});
    res.end();
  });

  // [Unrelated endpoints...]

  //listen on app port
  var port;
  var expr = _env.name == 'prod' ? port = process.env.PORT : port = _env.app.ai.port;
  server.listen(port);
  console.log('Listening to: '+port);
});

_util.configExpress:

self.configExpress = function(dirname, app, express){                                                                                                                                                                                        
  app.configure(function() {                                                                                             
    //setup jade                                                                                                         
    app.set('views', dirname+'/views')                                                                                   
    app.set('view engine', 'jade')                                                                                       
    //setup friendly console logging                                                                                     
    app.use(express.logger('dev'))                                                                                       
    //use body parser for handling http post                                                                             
    app.use(express.bodyParser());                                                                                       
    //define static file server                                                                                          
    app.use(express.static(dirname+'/'+_env.assets));                                                                    
  });                                                                                                                    
} 

But then when I test the endpoints:

$ curl -vv --user bst:bst localhost:3002/clf/create/bnn
*   Trying ::1...
* Connected to localhost (::1) port 3002 (#0)
* Server auth using Basic with user 'bst'
> GET /clf/create/bnn HTTP/1.1
> Host: localhost:3002
> Authorization: Basic YnN0OmJzdA==
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< X-Powered-By: Express
< Content-Type: text/plain
< Content-Length: 9
< Date: Thu, 02 Jul 2015 18:54:32 GMT
< Connection: keep-alive
< 
* Connection #0 to host localhost left intact
Not Found%  

$ -vv --user bst:bst localhost:3002/bst/drone/train
*   Trying ::1...
* Connected to localhost (::1) port 3002 (#0)
* Server auth using Basic with user 'bst'
> GET /bst/drone/train HTTP/1.1
> Host: localhost:3002
> Authorization: Basic YnN0OmJzdA==
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Date: Thu, 02 Jul 2015 18:54:45 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
< 
* Connection #0 to host localhost left intact
➜  reporting git:(feature/ai-train-api) ✗ 

Upvotes: 1

Views: 1335

Answers (1)

Patrick Murphy
Patrick Murphy

Reputation: 2329

You are using express version 3, you are trying to user route params like version 4.

req.params is an array not an object in version 3

Try looking here.

Code example

Try this I imagine that because var modelType would have been undefined that your ctrl,clfCreate() function failed to ever send the response. In the other endpoint you have res.end() outside the scope of your ctrl function

app.get('/clf/create/:modelType', _util.auth(express.basicAuth), function(req, res) {
    var modelParams = req.body ? req.body : {};
    var modelType = req.params[0];
    ctrl.clfCreate(modelType, modelParams, function(err, result) {
      if(err) {
        // at this point, assume bad request
        res.status(400);
        res.json({"err": err.message});
      } else {
        // if success, return classifier ID
        res.json({"id": result});
      }
      res.end();
    });
  });

Upvotes: 2

Related Questions