Not able to get data (node.js) from my client (react.js)

I’m not able to retrieve the data I’m sending from the client to the server.

My react code is the following:

axios.post('http://localhost:5000/post-entry', { data: formData })
.then(res => {
  console.log(res);
})

My server code is the following:

app.post('/post-entry', (req, res) => {
  console.log(req.data, "res.body.data here");
});

When it reaches the post on my server, the log is undefined.

What am I doing wrong?

>Solution :

If formData is a JS object, just axios.post('http://localhost:5000/post-entry', formData) is enough.
You can access this object on server side by req.body. But ExpressJS needs a middleware to parse request bodies. You have to use app.use(express.json()) or app.use(bodyParser.json()) where bodyParser is body-parser.

Leave a Reply