0

Here's the server controller (which works)

function logTheRequest(req, res) {
  console.log(req.body);
  res.sendStatus(200);
}

Here's the client side fetch

newCustomer() {
  fetch('/customers/sign-up', {
    method: 'POST',
    body: 'test',
  });
}

But when I console.log(req.body), all I get is { }

Tyler L
  • 835
  • 2
  • 16
  • 28
  • I think that it might expect json. Did you try to pass a contentType header (of 'application/text') ? – Kinnza Jan 01 '17 at 16:47
  • Just tried it with headers: { 'Content-Type': 'application/text' }, and it's still showing body { }. Thank you for offering a suggestion! :) – Tyler L Jan 01 '17 at 16:56
  • You might have a wrong nodejs configuration then. Check the network tab and see if the data was sent properly. – Kinnza Jan 02 '17 at 06:24
  • Should I make an issue on the github repo? It's react-starter-kit. Works with application/json, but not application/text – Tyler L Jan 02 '17 at 06:27
  • Im pretty sure the stack is intended to work with json, so there is no issue. If you wish to change it to text you need to adjust it. – Kinnza Jan 02 '17 at 06:38
  • 1
    If you are using - https://github.com/kriasoft/react-starter-kit, then check https://github.com/kriasoft/react-starter-kit/blob/master/src/server.js. it has this line of configuration : app.use(bodyParser.json()); – Kinnza Jan 02 '17 at 06:39

1 Answers1

0

req.body kept coming back empty for me until I added app.use(bodyParser.json() into my server.js file. Make sure you import body-parser.

Looks like the reason is because bodyParser parses request bodies in middleware under req.body: https://github.com/expressjs/body-parser. More details on bodyParser: What does `app.use(bodyParser.json())` do?.