I am working with nodejs/express. I want to do the same thing for a route, for GET and POST http requests.
I am doing this:
app.get('/', function(req, res) {
// Some code
});
app.post('/', function(req, res) {
// Same code
});
Is there a way to refactor get and post in the same callback ?
Thanks
Or you can use all
, if your site doesn't use any other methods in particular:
app.all('/', handler)
This seems like a very odd requirement. If the behavior is exactly the same just specify one function to handle both:
function myHandler(req, res) {
// Some code
}
app.get('/', myHandler);
app.post('/', myHandler);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With