2012-08-12 41 views
2

我想航线在多个文件中路线与多个文件的NodeJS

var routes=require('./routes'); 
在路线

/index.js

exports.inicio=require('./inicio') 
exports.home=require('./home') 
在inicio.js

exports.index=function(req,res){res.render('index/index',{title: 'Bienvenido a Inmoweb'});} 

在home.js

exports.nosotros=function(req, res){res.render('index/nosotros',{title:'Nosotros'});} 

当我CONSOLE.LOG(路由)

{ 
inicio: {index:[function]}, 
home: {nosotros:[function]} 
} 

,所以我呼吁在应用

app.get('/',routes.inicio.index); 

,但我想这样

app.get('/',routes.index); 
app.get('/nosotros',routes.nosotros); 

和执行console.log supose调用成为????

{ 
    index:[function], 
    nosotros:[function] 
} 

怎么办? TNX所有

回答

5

routes/index.js可以做到以下几点:

exports.index = require('./inicio').index 
exports.nosotros = require('./home').nosotros 

可以使这甚至在inico.js直接分配给module.exports短:

module.exports = function(req,res){res.render('index/index',{title: 'Bienvenido a Inmoweb'});} 

现在你可以做到这一点routes/index.js

exports.index = require('./inicio') //See the difference? 
// require('./inicio') now directly exports your route 
exports.nosotros = require('./home').nosotros 

想一想吗? :)

+0

这不是答案,但我解决了我的问题与你的帮助谢谢 – andrescabana86 2012-08-13 16:30:27

+0

安德烈,你的解决方案是什么。如果你更详细地说明你是如何解决问题的,那将会有所帮助。我认为rdrey的解决方案和解释非常好。 – TechplexEngineer 2012-12-22 04:22:52