Reputation: 770
Lets say that I would like to configure and use the passport
module.
In my app.js
, I have minimal module configuration like the following:
passport = require('passport'); // load module
app.use(passport.initialize()); // initialize passport
app.use(passport.session()); // use session
Now, I would like to configure routes for my app. However, I have this on a separate file, namely ./lib/router.js
. The question is whether I should be passing my configured passport
variable over to that file, or require
again the passport module in that file.
To illustrate:
// **app.js**
router = express.Router() // declare router
// option (A) Do I have to pass the passport variable to be using that same variable I have already defined in app.js?
app.use(require('./lib/router')(router, passport))
// **lib/router.js**
// option (B) or, can I just 'require' another passport module and affect the same module said in app.js?
passport = require('passport')
module.exports = (router) ->
router.get('/authenticate', passport.authenticate('local'))
I hope the explanation is clear. I would also like to know the "preferred" practice in such cases where a module is used in multiple locations (mongoose
is yet another module where different files require it to declare a model).
Thanks in advance.
Upvotes: 1
Views: 630
Reputation: 10346
Passport packages exports an instance of the Passport class, so if you do:
var passport = require('passport');
in your route, you will be using the same instance.
From the source code of Passport library:
exports = module.exports = new Passport();
Upvotes: 1