Express中的子文件夹样式路由 [英] Sub-Folder style routing in Express
本文介绍了Express中的子文件夹样式路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想解析这些简单的路线:
I want to parse simple routes like these:
http://example.com/foo/bar/baz/
其中没有理论上的限制。而且,它有一个数组 ['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.
如何使用Express路由?
How to do it with Express routing?
推荐答案
使用正则表达式。
app.get(/^\/((?:[^\/]+\/?)+)\//, function(req, res) {
res.send(req.params[0].split('/'));
});
app.listen(8080);
运行它然后
$ curl localhost:8080/foo/bar/baz/
["foo","bar","baz"]
这篇关于Express中的子文件夹样式路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文