I was trying to figure out how to get the request type from a node request. I want to perform different tasks based on the type.
module.exports = function(req, resp, next){
if (req.type == 'GET'){
//Do something
}else{
// Do else something
}
}
Express has a built-in express. json() function that returns an Express middleware function that parses JSON HTTP request bodies into JavaScript objects. The json() middleware adds a body property to the Express request req . To access the parsed request body, use req.
GET: GET request is used to read/retrieve data from a web server. GET returns an HTTP status code of 200 (OK) if the data is successfully retrieved from the server. POST: POST request is used to send data (file, form data, etc.) to the server.
req.method
returns the request HTTP method used.
app.use('/', (req, res, next) => {
let requestMethod = req.method;
console.log(requestMethod);
res.send('ok');
});
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