Question on req.body with react

Question on req.body with react
0

#1

Hi…I am new to react js…how to extract data from req.body?is it accessible outside app.post?


Javascript's Fetch API - posting data to server body req.body is always empty?
#2

Hello, you originally posted your question in an older thread, but I have moved it to a new one as it was somewhat unrelated to the old thread and you should get faster help this way.


I don’t think req.body is concerned with React, as I’m guessing you are referring to your express API or server. Once you include the body parser middleware in your express app (npm install body-parser and the require and use it), you can access it with req.body inside app.post etc. like you mentioned.

app.post('/signup', (req, res) => {
  const body = req.body;
});

I am not sure why you want to access it outside of the app.post function, but I think you could by assigning it to a variable in a higher scope. You would then have to worry about it being empty with asynchronous callbacks and such.

let body = {};

app.post('/signup', (req, res) => {
  body = req.body;
});

Can you provide more info on what you are trying to do?