The express 4x api docs claim that you can pass regex as a second argument to router.param
in order validate params.
The method could now be used to effectively validate parameters (and optionally parse them to provide capture groups)
It then provides the following examples.
// validation rule for id: should be one or more digits
router.param('id', /^\d+$/);
router.get('/user/:id', function(req, res) {
res.send('user ' + req.params.id);
});
// validation rule for range: should start with one more alphanumeric characters, followed by two dots, and end with one more alphanumeric characters
router.param('range', /^(\w+)\.\.(\w+)?$/);
router.get('/range/:range', function(req, res) {
var range = req.params.range;
res.send('from ' + range[1] + ' to ' + range[2]);
});
But, this doesn't actually seem to work. Taking a deeper dive, it doesn't look as if the express code actually supports what the docs claim. In fact, passing anything other than a function will get you a tidy invalid param() call
exception.
express 4.12.3
node 0.12.4
So my question is whether this functionality actually exists or if I'm doing something wrong. I'm trying to accomplish the same thing provided in the doc but am receiving the error mentioned above. Any guidance would be greatly appreciated :)
The answer can be found here.
Essentially the following snippet would need to be run prior to leveraging the router.param(fn)
method as outlined above if you're using express <= 4.11
.
4.11
router.param(function(name, fn) {
if (fn instanceof RegExp) {
return function(req, res, next, val) {
var captures;
if (captures = fn.exec(String(val))) {
req.params[name] = captures;
next();
} else {
next('route');
}
}
}
});
4.12
If you're using express >= 4.12
you can accomplish the same without the need of router.param(fn)
using the following. In fact, the pre 4.12 example above will pop a deprecation warning.
app.get('/user/:userId([0-9]+)', fn);
While this is stated in the doc, it isn't quite clear.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With