What is the difference between "express.Router" and routing using "app.get"?
Here's a simple example:
// myroutes.jsvar router = require('express').Router();router.get('/', function(req, res) { res.send('Hello from the custom router!');});module.exports = router;
// main.jsvar app = require('express')();app.use('/routepath', require('./myroutes'));app.get('/', function(req, res) { res.send('Hello from the root path!');});
Here, app.use()
is mounting the Router
instance at /routepath
, so that any routes added to the Router
instance will be relative to /routepath
.