2

i want to route in multiple files

var routes=require('./routes');

in the routes/index.js

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

in the inicio.js

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

in the home.js

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

when i console.log(routes)

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

so i call in app

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

but i want to call like this

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

and the console.log supose to be????

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

how to do that??? tnx all

andrescabana86
  • 1,778
  • 8
  • 30
  • 56

1 Answers1

5

Your routes/index.js can do the following:

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

You can make this even shorter by assigning directly to module.exports in inico.js:

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

Now you can do this in routes/index.js:

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

Get the idea? :)

rdrey
  • 9,379
  • 4
  • 40
  • 52