- First step is create a separate folder for routes and lets name it as "routes".
- Next create
index.js
file in "routes" directory
After this step the project structure will look like this.
|--routes | |-index.js |--views | |-default.js |--app.js
- Export routes in
index.js
file.
exports.index = function(req,res){ res.render('default',{title:"Home"}); }
- Require the routes folder in
app.js
file
var routes = require('./routes');
- Provide methods in
index.js
file toget
method of Express when cofiguring routes
app.get('/',routes.index);
In this way we can modularize our routes in node.js app and we can have a clean and understandable code.