Some syntax sugar. Would be cool, if express would parse RegEx's as a part of routing paths.
Example:
"/page/:page([0-9])"
"/skin/:direction([a-z])?/:resolution([0-9])?/:username"
Eveything in the braces should be parsed as a RegEx. That way more complex, but sweetend, paths could be built. :)
I know, that you can pass in RegEx's as a path, but then you'll end up with req.params as an array.
Yeah or No? Maybe I'll make a PR.
I would love to do something like /(page|post)/:id
That would be some kind of enum. I had that in mind aswell. That would internally translate to: /:path0(page|post)/:id.
Hmm.
Isn't app.param used for this? Of course app.param callbacks will execute for each route but if you want it only for a specific route, just add another middleware before you main callback and if the parameter doesn't match your regex run next('route').
app.get("/page/:page", function(req, res, next) {
if (!/^[0-9]+$/.test(req.params.page) next('route');
next();
}, function(req, res, next){
//...code that only run when `page` parameter is a integer
});
Uhm, yeah. That's a pssible work around, but it doesn't really look good and isn't as simple to use, as the proposed format.
Currently I pass in RegEx's as paths, and use the req.params array. But passing in a string and then using a real object seems cooler. :D
up! this would be very useful to me too.
also, I would find useful specifying arrays of strings or regular expressions. for example, I would like to write:
app.get([
'/page/:id',
'/post/:id'
], function (request, response) {
// ...
});
alternatively to:
app.get(/^\/(page|post)\/:id$/, function (request, response) {
// ...
});
@71104 Same as this one. :wink:
yeah, but I would also like the alternative of the arrays, at least for a matter of readability. :)
@71104 Ah, alright then. I openend a seperate issue for this. I think, I'll do a PR anytime soon and will include this aswell.
stuff like /page/:page([0-9]) already works, you just have to double-escape slashes since it's s string
I don't get it. How do I have to change the string to make this route only catch urls of the form /\/page\/([0-9]+)/, where as ([0-9]+) should be exposed as req.params.page?
app.VERB("/page/:page([0-9]+)", function(req, res) {
console.log(req.params.page);
});
just like you have right there
Nevermind. :D
express-params kinda does, what I want. I just have to keep names consistent.
There is a way to check param length with a regex in the route like:
app.VERB("/:id({24})", function(req, res) {
console.log(req.params.id);
});
Because i get
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
^
SyntaxError: Invalid regular expression: /^\/(?:({24}))\/?$/: Nothing to repeat
using express 3.0.0
like this:
server.get('/:id([a-zA-Z0-9]{24})', function(req, res) {
console.log(req.params.id);
res.end();
});
thanks @rump ! it works
Most helpful comment
Hmm.
Isn't
app.paramused for this? Of courseapp.paramcallbacks will execute for each route but if you want it only for a specific route, just add another middleware before you main callback and if the parameter doesn't match your regex runnext('route').