Including route handlers in multiple files-Node.js

When you have to define multiple routes in different files how will you get app.js file to recognize all route handlers?. A require cannot handle it. You need to create routes.js file.

module.exports =function(app){

app.get('/login', function(req, res){

res.render('login', {

title: 'Express Login'



//other routes..


Then require it from app.js by passing the app object as given below.


In the case of express 4.x get an instance of the router object and import another file which contains routes. Doing this recursively will allow you to create easy to maintain URL paths. It is convenient since it lets you to logically group the routes by URL path.