我有以下代码:
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.
我有什么选择?
谢谢.
只需将中间件升级到自己的功能,并在两个路径中使用它.
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)
| 归档时间: |
|
| 查看次数: |
2105 次 |
| 最近记录: |