In app.js I have:
const routes = './routes/'
const usersRouter = require(routes +'users');
/*more code*/
app.use('/users', usersRouter);
In users.js, I have:
const express = require('express');
const router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;
My question: How does router.get('/')
know to get the users file? Usually I would need to pass '/users'
to router.get(
. But with app.use('/users'
in app.js, all I need is router.get('/')
in users.js. In fact if I type router.get('/users'
in users.js the program breaks.
Learning Express, and I could use explanation of how this works.