- First step is create a separate folder for routes and lets name it as "routes".
- Next create
index.jsfile 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.jsfile.
exports.index = function(req,res){ res.render('default',{title:"Home"}); }
- Require the routes folder in
app.jsfile
var routes = require('./routes'); - Provide methods in
index.jsfile togetmethod 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.
