[英]How to have multiple routes use the same route function in express.js
I can use 我可以用
app.get('/:someVar/xxxxx', function(req, res) { /* etc */ });
to get someVar by req.params.someVar
. 通过
req.params.someVar
获得req.params.someVar
。 However, I want both www.example.com/12345/xxxxx
and www.example.com/xxxxx
to go into the same app.get
但是,我希望
www.example.com/12345/xxxxx
和www.example.com/xxxxx
都进入同一个app.get
How should I approach this problem? 我应该如何解决这个问题?
Assign function to variable 将函数分配给变量
var yourFunction = function (req, res) {
...
}
And you can use it afterwards as parameter passed to app.get()
然后可以将其用作传递给
app.get()
参数
app.get('/:someVar/xxxxx', yourFunction);
app.get('/xxxxx', yourFunction);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.