节点快递:某些路线上有404但不是其他路线

时间:2015-07-02 19:02:52

标签: node.js express routing

所以我有一个问题,我的一些express.js(v3.2.0)路由返回404状态代码,而其他路由没有。我无法弄清楚为什么有些路线工作正常,而其他路线没有解决。任何帮助将不胜感激。

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));                                                                    
  });                                                                                                                    
} 

但是当我测试端点时:

$ 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) ✗ 

1 个答案:

答案 0 :(得分:2)

您正在使用快速版本3,您正尝试使用版本4的路径参数。

req.params是一个数组,而不是版本3中的对象

尝试查看here

代码示例

尝试这个我想象因为var modelType未定义你的ctrl,clfCreate()函数未能发送响应。在另一个端点中,您的res.end()超出了ctrl函数的范围

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();
    });
  });