expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.68k stars 16.28k forks source link

feat: Support typed params in routes #2756

Open diosney opened 9 years ago

diosney commented 9 years ago

I mean, do something like:

app.route('/:an_id{number}');
app.route('/:another_id{string}')

which is roughly equivalent to use regex in each case, but the shorthand above is clearer and allows req.params to properly type the params, fi: req.params.an_id would be a number instead of a string.

Thanks

aredridel commented 9 years ago

Is there prior art this derives from?

diosney commented 9 years ago

@aredridel Yes, but I don't recall where I saw it.

diosney commented 9 years ago

@aredridel angular ui-router module has this feature :smile: I know is front-end but at the backend we can give it a good use of it (thought the syntax is different that the one I added above).

https://github.com/angular-ui/ui-router/wiki/URL-Routing

gabeio commented 9 years ago

I have seen this in other express-similar libraries like bottlepy & flask.

vladvelici commented 8 years ago

I implemented some support for this in path-to-regexp, the library that parses the paths into regular expressions.

My commit is here. I didn't sent a pull request yet as there is no issue related to it in path-to-regexp. I will open one and try to get some feedback.

blakeembrey commented 8 years ago

Interesting. On a (partially related) side note, I did something similar for https://github.com/pillarjs/router/pull/29 (the standalone router, not in Express 4.x) which added support for the RAML type system using https://github.com/mulesoft-labs/raml-path-match. If something like that lands, it would be straightforward to add a new path matching implementation with types (I know URI templates is of some interest too).

Edit: That router is here: https://github.com/mulesoft-labs/osprey-router