Reputation: 5069
app.js
const adminRoutes = require('./routes/admin');
app.use('/admin/customer/:customerId', (req, res, next) => {
console.log('middleware id=', req.params.customerId);
next();
}, adminRoutes);
and here is routes/admin.js
const express = require('express');
const router = express.Router();
router.post('/user', (req, res) => {
console.log('route id=', req.params.customerId);
res.json(req.params);
});
module.exports = router;
I am getting following output
middleware id= 1
route id= undefined
while expected output is
middleware id= 1
route id= 1
Upvotes: 0
Views: 51
Reputation: 6232
You can try this:
You can directly call your route without using another callback and it should work
app.js
const adminRoutes = require('./routes/admin');
app.use('/admin/customer/:customerId',adminRoutes);
If it does't work then you will have to set the params in request object before using your routes
Upvotes: 0
Reputation: 8060
You need to use Router
options
property mergeParams
if you want to access params
from the parent router.
const express = require('express');
const router = express.Router({mergeParams:true});
router.post('/user', (req, res) => {
console.log('route id=', req.params.customerId);
res.json(req.params);
});
module.exports = router;
Upvotes: 2