Allow for multiple parameter names in router.param()
See original GitHub issueHi,
I just found out about router.param() and immediately started using it in my code. It makes checks based on parameters so darn clean 😃 Really good job on the inventor of that idea!
While using it i found myself wondering with a url like: “/:year/:month/:day”. How can you make a router.param(…) function that is called when exactly those 3 arguments have been provided in the url? Perhaps it is possible, but i haven’t seen a way in the documentation.
Since router.param() works on parameter names, i would suggest a feature like the following to allow for an implementation of the above to be possible in router.param().
router.param(['year', 'month', 'day'], (req, res, next, obj) => {
obj.year; // contain the year value
obj.month; // contain the month value
obj.day; // contain the day value
});
I would say that the first array in which the params are provided as string must be the exact order in which they would occur in the app.get(‘/:year/:month/:day’ …) function;
Is there any technical (or practical) reason why a feature like this wouldn’t be possible? What are your thoughts on this?
Best regards, Mark
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:6
Having briefly looked into the source code of
router.param()
(which isproto.param()
inrouter/index.js
), I noticed that all currentrouter.param()
did is push the callback on therouter.params[paramName]
array, and does nothing else.router.handle()
, a not documented method, does the actual work. It calls another internal methodrouter.process_params()
, that actually invokes the pushed callbacks on each parameter one by one, sequentially without repeat. Fromindex.js
:I believe to add the feature you asked may require changing these structures, and I don’t feel of that much necessity if you can just give the job to a normal
app.use()
like methods.It would be good to have the
router.param()
accepting multiple middlewares, that will permit us to split our middlewareexample: