Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Node.js request type?

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
    }
}
like image 941
Jesse Jaime Avatar asked Apr 12 '15 18:04

Jesse Jaime


People also ask

How do I get an Express Body request?

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.

What are the different types of HTTP requests in node JS?

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.


2 Answers

req.method returns the request HTTP method used.

like image 98
Miguel Mota Avatar answered Oct 07 '22 02:10

Miguel Mota


app.use('/', (req, res, next) => {
  let requestMethod = req.method;
  console.log(requestMethod);
  res.send('ok');
});
like image 34
skw.success Avatar answered Oct 07 '22 00:10

skw.success