Node.js Express路由器中的正则表达式 [英] Regular Expression in Node.js Express Router
本文介绍了Node.js Express路由器中的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到一种将正则表达式输入到快速路由URL中,然后通过请求对象访问URL的可变部分的方法.具体来说,我想路由到网址"/posts/" +任何数字.有办法吗?
I have tried to find a way to enter regular expression into an express routing URL and then access the variable portion of the URL through the request object. Specifically I want to route to the url "/posts/" + any number of digits. Is there a way to do this?
示例:
/posts/54
/posts/2
/posts/546
推荐答案
这应该做到:
app.get('/posts/:id(\\d+)', function(req, res) {
// id portion of the request is available as req.params.id
});
编辑:在路径中添加了正则表达式以将其限制为数字
EDIT: added regex to path to limit it to digits
这篇关于Node.js Express路由器中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文