How to declare a mandatory parameter in node.js/express router -
i got series of routes :
var router = express().router; router.get('/',middleware1,middleware2); router.get('/courses',middleware1,middleware2,..) router.post('/dates',middleware1,middleware2,..) app.use('/u',router);
now, when declare root of route in
app.use('/u',router);
i put mandatory parameter has present in every route of micro-app like
app.use('/u/:user_name/',router);
so can check every route router.param existence of param in db.
is possible defining param in root of micro app , not in every single route like
router.get('/:user_name/',middleware1,middleware2); router.get('/:user_name/courses',middleware1,middleware2,..) router.post('/:user_name/dates',middleware1,middleware2,..)
as lot in real application , pain change them all?
really !
you should able using mergeparams
long you're using recent enough version of express.
var router = express.router({mergeparams: true});
then work:
app.use('/u/:user_name/', router);
mergeparams
documented here:
Comments
Post a Comment