I want to parse simple routes like these:
http://example.com/foo/bar/baz/
where there is no theoretical limitation to the number of them. And it would be nice to have an array ['foo','bar','baz'] from it.
How to do it with Express routing?
Use a regular expression.
app.get(/^\/((?:[^\/]+\/?)+)\//, function(req, res) {
res.send(req.params[0].split('/'));
});
app.listen(8080);
Run it and then
$ curl localhost:8080/foo/bar/baz/
["foo","bar","baz"]
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