Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  116] [ 7]  / answers: 1 / hits: 8373  / 10 Years ago, thu, may 1, 2014, 12:00:00

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.


More From » node.js

 Answers
13

Move 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!
});

[#45617] Wednesday, April 30, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tonisandyp

Total Points: 694
Total Questions: 97
Total Answers: 77

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;