2011-09-09 53 views
0

我正在使用'express-namespace'來對我的路由進行分類。 這是我的代碼。「undefined不是函數」in nodejs/expressjs

.. 
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文件和 我所有的控制器映射爲相應的小寫所以我的應用程序的網址是 非常整潔。

這對每次運行'node app.js'時的第一次POST都是相當不錯的。 如果我張貼到URL第二次,以下異常 發生:

TypeError: undefined is not a function 
    at CALL_NON_FUNCTION (native) 
    at /home/carl/source/node/funner/app.js:62:21 
    at callbacks (/usr/local/lib/node/.npm/express/2.4.6/package/lib/router/index.js:272:11) 
    at param (/usr/local/lib/node/.npm/express/2.4.6/package/lib/router/index.js:246:11) 
    at pass (/usr/local/lib/node/.npm/express/2.4.6/package/lib/router/index.js:253:5) 
    at Router._dispatch (/usr/local/lib/node/.npm/express/2.4.6/package/lib/router/index.js:280:4) 
    at Object.handle (/usr/local/lib/node/.npm/express/2.4.6/package/lib/router/index.js:45:10) 
    at next (/usr/local/lib/node/.npm/connect/1.7.0/package/lib/http.js:201:15) 
    at /usr/local/lib/node/.npm/connect/1.7.0/package/lib/middleware/session.js:323:9 
    at /usr/local/lib/node/.npm/connect/1.7.0/package/lib/middleware/session.js:342:9 

誰能給我一個提示嗎?

回答

1

我沒有看到明確的錯誤,但確實看到了一些危險的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 }); 
0

我沒有使用過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', ...);

做告訴我,如果它的工作原理。

PD。我是node+express的新手。

相關問題