我试图找到一种方法,将正则表达式输入到快速路由 URL 中,然后通过请求对象访问 URL 的可变部分。具体来说,我想路由到 url "/posts/"+ 任意数字。有办法做到这一点吗?
例子:
/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
});
编辑:将正则表达式添加到路径以将其限制为数字
关于regex - Node.js Express 路由器中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12123564/