I have structured my application similar to : Express Code Structure by @focusaurus.
So I have like this in my app.js file.
#!/usr/bin/env node
var express = require('express');
# ... truncate other code for brevity
var app = express();
var router = express.Router();
# ... truncate other code for brevity
[
'app/home/routes',
'app/auth/routes'
].forEach(function (routePath) {
require(routePath)(router);
});
app.use('/', router);
module.exports = app;
What I have in mind is that, only routes that specifically needs to use passport middleware will have to require passport. As you can see in many articles all over the internet, what they tend to do is:
var passport = require('passport');
app.use(passport.initialize());
app.use(passport.session(config));
And then use passport as a parameter in a route file, like so:
require('app/routes')(app, passport)
So that routes should be able to use it if needed
app.get('/login', passport.authenticate('local') ...);
What could be a better approach, to this kind of problem. In my authentication routes, I actually did this.Authentication Route, app/auth/routes.js
... #truncate for brevity
function setup(router) {
router.get ..
router.post ..
}
module.exports = setup;