Node.js &引用;“未定义”不是一个函数;在nodejs/expressjs中

Node.js &引用;“未定义”不是一个函数;在nodejs/expressjs中,node.js,express,Node.js,Express,我正在使用“express namespace”对路由进行分类。 这是我的密码 .. 9 var controllers = require('./controllers'); 10 require('express-namespace'); .. 46 47 app.namespace('/json', function(){ 48 app.post('/', function(req, res, next){ 49 res.header('Content-

我正在使用“express namespace”对路由进行分类。 这是我的密码

..
 9 var controllers = require('./controllers');
 10 require('express-namespace');
..
 46
 47 app.namespace('/json', function(){
 48     app.post('/', function(req, res, next){
 49         res.header('Content-Type', 'application/json');
 50         next();
 51     });
 52     /**
 53      * Map the controller objects and its actions
 54      * to the corresponding URL in lower case
 55      */
 56     for(var controller in controllers){
 57         app.namespace('/' + controller.toLowerCase(), function(){
 58             controller = controllers[controller];
 59             for(var action in controller){
 60                 app.post('/' + action.toLowerCase(), function(req,res){
 61                     action = controller[action];
 62                     action(req, function(result){
 63                         res.send(result);
 64                     });
 65                 });
 66             }
 67         });
 68     }
 69 });
下面是我的./controllers.js代码:

...
 4 var Users = {
 5 };
 6
 7 Users.create = function(req, result){
...
 22 }
 23
 24 exports.Users = Users;
 25
我的重点是将控制器代码移动到单个.js文件和映射中 将我的所有控制器转换为相应的小写URL,以便我的应用程序 相当整洁

每次运行“node”时,第一篇文章的效果都非常好 app.js’。 如果我第二次发布到URL,以下异常 发生:


有人能给我一个提示吗?

我没有看到明显的错误,但我确实看到一些危险的javascript。for(key in obj)语句应该由hasOwnProperty ifs过滤,使用var语句在循环中的函数中定义局部变量的范围非常重要

 47 app.namespace('/json', function(){
 48     app.post('/', function(req, res, next){
 49         res.header('Content-Type', 'application/json');
 50         next();
 51     });
 52     /**
 53      * Map the controller objects and its actions
 54      * to the corresponding URL in lower case
 55      */
 56     for(var controller in controllers){
            **if(controller.hasOwnProperty(controller) {**
 57         app.namespace('/' + controller.toLowerCase(), function(){
 58             **var mycontroller** = controllers[controller];
 59             for(var action in mycontroller){
                    **if(mycontroller.hasOwnProperty(action) {**
 60                 app.post('/' + action.toLowerCase(), function(req,res){
 61                     **var myaction** = mycontroller[action];
 62                     myaction(req, function(result){
 63                         res.send(result);
 64                     });
 65                 });
                    }
 66             }
 67         });
            }
 68     }
 69 });

我没有使用
express namespace
,但我认为您可以使用
req.params
来映射控制器的URL。像这样:

app.namespace('/json', function(){

  app.post('/:controller/:action', function(req, res, next){

    var controller = req.params.controller,
            action = req.params.action;

        controller[0] = controller[0].toUpperCase();

    if( controllers[ controller ][ action ] ){
      res.header('Content-Type', 'application/json');

      controllers[ controller ][ action ](req, function(result){
        res.send(result);
      });

    }
    else
      res.send('Action %s is not defined for the %s controller', action, controller);    


  });

});
对app.post('/:controller',…)也可以这样做

告诉我它是否有效

警察局。我不熟悉
node+express

app.namespace('/json', function(){

  app.post('/:controller/:action', function(req, res, next){

    var controller = req.params.controller,
            action = req.params.action;

        controller[0] = controller[0].toUpperCase();

    if( controllers[ controller ][ action ] ){
      res.header('Content-Type', 'application/json');

      controllers[ controller ][ action ](req, function(result){
        res.send(result);
      });

    }
    else
      res.send('Action %s is not defined for the %s controller', action, controller);    


  });

});