【发布时间】:2016-11-12 18:32:21
【问题描述】:
我正在尝试通过检查 cookie 为我的应用创建身份验证机制。所以我添加了router.all('*') 来匹配每个请求,检查cookie,然后转到实际的处理程序。但我需要它只匹配在“/”之后有一个或多个字符的请求。 (我想匹配/manage、/m 或/about,但不是/)。
//Assuming a request for '/', this gets called first
router.all('*', function (request, response, next) {
//Stuff happening
next();
});
//This gets called on next(), which I do not want
router.get('/', function (request, response) {
//Stuff happening
});
//However this I do want to be matched when requested and therefore called after router.all()
router.post('/about', function (request, response) {
//Stuff happening
});
根据答案here,您可以使用正则表达式进行路径匹配,但我真的不明白'*' 是什么。它可能匹配所有内容,但对我来说看起来不像正则表达式。
- 为什么
'*'匹配/?
- 我需要向
all()提供什么参数才能匹配/about而不是/?
【问题讨论】:
标签: javascript regex node.js express