In the scenario presented below, how can I access the 'pathId' within the 'router.use' function? Is it feasible or do I need to implement a different approach?
var router = require('express').Router();
function someMiddleware(id) {
return (req, res, next) => {
// perform operations with id
next();
}
}
router.use('/:pathId', someMiddleware(pathId) /*, add more middleware */); // not functioning
The rationale behind avoiding direct access to req.params.pathId
in the 'someMiddleware' function is to keep the middleware independent of the specific parameter name, enabling its use in various configurations.
Edit
A workaround solution involves using a wrapper middleware. The implementation would resemble the following:
router.use('/:pathId',
(req, res, next) => someMiddleware(req.params.pathId)(req, res, next));
This method allows 'someMiddleware' to operate without needing knowledge of the specifics of req.params
.