Move the rout definition from the index.js or app.js
Folder structure
index.js file
var express = require('express'); var app = express(); app.use(express.static('public')); //Routes app.use(require('./routes')); //http://127.0.0.1:8000/ http://127.0.0.1:8000/about //app.use("/user",require('./routes')); //http://127.0.0.1:8000/user http://127.0.0.1:8000/user/about //you can create more routs //app.use("/admin",require('./adminroutes')); //http://127.0.0.1:8000/admin http://127.0.0.1:8000/admin/about var server = app.listen(8000, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
routes.js
var express = require('express'); var router = express.Router(); //Middle ware that is specific to this router router.use(function timeLog(req, res, next) { console.log('Time: ', Date.now()); next(); }); // Define the home page route router.get('/', function(req, res) { res.send('home page'); }); // Define the about route router.get('/about', function(req, res) { res.send('About us'); }); module.exports = router;
you can add more routs like admin routs (*i am not added this in my project so it will not see int the above folder structure)
adminroutes.js
var express = require('express'); var router = express.Router(); //Middle ware that is specific to this router router.use(function timeLog(req, res, next) { console.log('Time: ', Date.now()); next(); }); // Define the home page route router.get('/', function(req, res) { res.send('Admin dash board'); }); // Define the about route router.get('/profile', function(req, res) { res.send('About profile'); }); module.exports = router;