通过variables来expression3条路线

我有一个ExpressJS 3应用程序,并在一个单独的文件中有我的路线。 我想在我的app.js中声明路由文件中可用的一些configuration – 在路由定义本身之外。

在我的app.js我想做一些事情,如:

app.set('path_to_models', '/path/models/'); 

然后在我的routes / index.js文件,我想能够做到这一点:

 var Product = require(app.get('path_to_models') + 'product'); exports.list = function(req, res){ return Product.find(function (err, products) { if (!err) { res.render('product/manage', { products: products }); } else { res.render('5xx'); } }); }; 

我已经看到了一些相关的post,但没有真正解决我在找什么。 我知道我可以在一个函数中包装路线,但是如果可能的话,我想要一个替代方法来保持我的代码。

谢谢

我只是build立一个独立的config模块来保存我的configuration,并且在任何需要来自configuration信息的模块中,我只是要求它正常。 当更松散耦合的方法工作得很好时,为什么要将快递拖入混合中。

config.js

 exports.pathToModels = "/path/to/models"; 

routes.js

 var config = require("./config"); console.log(config.pathToModels); 

只需将app作为parameter passing给`routes / index.js':

 var routes = require('./routes/index.js')(app); 

更新:这应该是

 var routes = require('./routes/index.js').init(app); 

并在routes/index.js

 var app; exports=function(whichApp) { 

更新:这应该是

 exports.init=function(whichApp) { app=whichApp; // do initialization stuff return exports; } exports.list=...