2014-04-07 22 views
0

項目結構的NodeJS - 如何在任何文件

app.js 
lib 
    model 
     User.js 
     Product.js 
routes 
    ..... 

我想執行

app.param('PARAMNAME'); 

每個模型默默文件使用Express實例爲單(?)。 但我無法在模型文件中顯示js實例。

如何在沒有像「init(app)」之類的方法的情況下執行此操作?

回答

1

一種方法是通過你的控制器。您可以訪問控制器(路由)中請求對象中的app對象。從您的控制器中,您可以將app對象或特定值app.param('PARAMNAME')傳遞給您的模型。這種方法你的代碼看起來或多或少是這樣的:

在你app.js

app.get('/someurl', someController.someMethod); 

在someController.js

exports.someMethod = function(req, res) { 
    // you have access to the "app" object here 
    var paramName = req.app.param('PARAMNAME'); 
    // pass the value to your model 
    yourModel.someMethod(paramName); 
    // the rest of your controller code 
}