Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sub-Folder style routing in Express

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?

like image 499
Lanbo Avatar asked Feb 05 '26 08:02

Lanbo


1 Answers

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"]
like image 194
fent Avatar answered Feb 07 '26 23:02

fent