如何让多个路由在express.js中使用相同的路由功能

时间:2014-11-16 04:01:50

标签: javascript node.js express

我可以用

app.get('/:someVar/xxxxx', function(req, res) { /* etc */ });

通过req.params.someVar获取someVar。但是,我希望www.example.com/12345/xxxxxwww.example.com/xxxxx同时进入app.get

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

将函数分配给变量

var yourFunction = function (req, res) {
...
}

然后您可以将其用作参数传递给app.get()

app.get('/:someVar/xxxxx', yourFunction);
app.get('/xxxxx', yourFunction);

答案 1 :(得分:0)

不要重复自己。将数组传递给express.js的路由方法:

app.route(["/12345/xxxxx", "/xxxxx"])
   .get(function (req, res) { /* etc */ })

app.route& app.get