Expose vs Creating object in Router file of Nodejs

我怕爱的太早我们不能终老 提交于 2019-12-03 09:08:08

You could wrap your router.js in a function that accepts the passport object as parameter and handles the instantiation of the router also. Then, module.export that function instead of only the router.

EDIT: including example

server.js:

var passport = require('passport');
var router = require('./app/routes')(app, passport);
app.use('/', router);
app.listen(8080);

routes.js:

var express = require('express');
module.exports = function(app, passport){

   var router = express.Router();

   // routes go here
   // do stuff with passport

   return router;

}

this way works

    var express = require('express')
  , home = require(_pathtoyourroutes.js)
app.get('/', home.index);

//router.js

exports.index = function(req, res){
    var d = new Date();
  res.render('index', { title: 'Hello world', description:'Hello world this is a awesome site.', stylesheet: 'body.css', jsfile: 'home.js' });
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!