admin管理员组文章数量:1314514
In Node/express I have a POST
request that if it contains an id
I would like it to call the PUT
method instead. No redirect, just how to call the put method from the post method?
router.put('/:id', function(req, res) {
// code ...
});
router.post('/:id?', function(req, res) {
if (req.params.id) {
// call PUT method
}
});
I don't want to do a redirect, just make it as if it was part of the current request.
In Node/express I have a POST
request that if it contains an id
I would like it to call the PUT
method instead. No redirect, just how to call the put method from the post method?
router.put('/:id', function(req, res) {
// code ...
});
router.post('/:id?', function(req, res) {
if (req.params.id) {
// call PUT method
}
});
I don't want to do a redirect, just make it as if it was part of the current request.
Share Improve this question asked May 1, 2014 at 20:08 RobRob 11.4k22 gold badges72 silver badges114 bronze badges1 Answer
Reset to default 7Move the code to a named function and call that instead.
function handlePut(req, res) {
// code ...
}
router.put('/:id', handlePut);
router.post('/:id?', function(req, res) {
if (req.params.id) {
return handlePut(req, res);
}
// don't forget to handle me!
});
本文标签: javascriptHow to call PUT router from POST router in Nodejs ExpressStack Overflow
版权声明:本文标题:javascript - How to call PUT router from POST router in Node.js Express? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741965804a2407541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论