Reputation: 1758
I'm refactoring my router in express app, and I'm facing an issue regarding the params of the query. I splited my routes into multiple files, and created router modules for each.
These routes are registered in an index.ts :
import focRoutes from './foc';
import fedRoutes from './fed';
const router = Router();
router.use('/foc', focRoutes);
router.use('/foc/:foc_id/fed', fedRoutes);
export default router;
Then defined :
// foc.ts
const router = Router();
router.get('/', () => {...})
router.post('/', () => {...})
export default router;
// fed.ts
const router = Router();
router.get('/', () => {...})
router.post('/:fed_id', (req) => {
console.log(req.params.foc_id); // <- this logs `undefined`
console.log(req.params.fed_id); // <- this successfully logs the id
})
export default router;
The issue is that the req.params.foc_id
set in the index.ts
becomes undefined once in fed.ts
.
Previously I had all the routes in the same file, and doing this worked well :
router.post('/foc/:foc_id/fed/:fed_id', (req) => {
// Can use `req.params.foc_id` and `req.params.fed_id` without issue
});
Since I put the :foc_id
param in the index.ts
file, it's not available from the associated route file. If I move back the param to fed.ts
it works well
router.post('/:foc_id/fed/:fed_id', () => {}) // <- no issue
Is it normal behavior to lose the param along the chain of router modules ? I couldn't find anything about this issue and don't know what to do.
Upvotes: 1
Views: 20