转发到另一个路由处理程序而不在express中重定向

Mic*_*ael 2 node.js express

我有以下代码:

app.get('/payment', function(req, res) {
  // do lots of stuff
});
Run Code Online (Sandbox Code Playgroud)

现在我想添加以下内容:

app.post('/payment', function(req, res) {
  req.myvar = 'put something here';
  // now do the same as app.get() above
});
Run Code Online (Sandbox Code Playgroud)

显然我想重用代码.我尝试next('/payment')在post处理程序内部并将其放在get处理程序之上,但没有运气,可能是因为它们是不同的VERB.

我有什么选择?

谢谢.

And*_*ren 5

只需将中间件升级到自己的功能,并在两个路径中使用它.

function doLotsOfStuff (req, res) {
  // do lots of stuff
}

app.get('/payment', doLotsOfStuff);

app.post('/payment', function(req, res, next) {
  req.myvar = 'put something here';
  next();
}, doLotsOfStuff);
Run Code Online (Sandbox Code Playgroud)