What's the best way to access the express 'app' object from inside a separate route file?

express, javascript, node.js

Solution

You can simply access app by req.app in your route handlers

Problem

In Express 4, by default, routes are loaded from a separate file: ``` app.use('/', routes); ``` Would load `routes/index.js`. I have a third-party library that attaches to `app` itself. Is there a preferred way to access `app` from inside `routes/index.js`? I've thought about dependency injection ie, `routes/index.js` does ``` module.exports = function(app){ (routes go here) } ``` and then: ``` app.use('/', routes(app)) ``` But I wonder if there's a better way. What's the best way to access the express 'app' object from inside a separate route file?

Original source