简体   繁体   English

Express中的子文件夹样式路由

[英]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. 从它那里得到一个数组['foo','bar','baz']会很高兴。

How to do it with Express routing? 如何使用Express路由?

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM