2012-08-12 34 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