How to access POST data in express js?

Issue

Im using (in the browser):

  xmlHttp.open("POST", "/", true); // true for asynchronous

  xmlHttp.send("data");

on the client side browser.

I’m using (node js):

app.post("/", function (req, res) {
  console.log("Got a POST request");
  console.log(req);
});

to get the post data. but it doesn’t matter what I send it doesn’t show up.
I don’t wanna have to install some separate "body parser" package to see the data…. I don’t wanna parse it. A post request is used to send data to the server… is the data just not there?

Solution

I don’t wanna have to install some separate "body parser" package to see the data…. I don’t wanna parse it.

Too bad.

Even if you want to process a plain text body then you need to use body parsing middleware to do it.

One of the responsibilities of the middleware is to take the stream of data from the client and store it in memory (assigning it to req.body) instead of discarding it.

Note that body parsing middleware is installed automatically as a dependency of Express.js itself.

Answered By – Quentin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published