Quantcast
Channel: Express.js req.body undefined - Stack Overflow
Viewing all articles
Browse latest Browse all 54

Answer by Greggory Wiley for Express.js req.body undefined

$
0
0

In express 4 and above you don't need body parser they have their own json parse method,At the higehset level of your express app add

var express = require('express');var app = express()app.use(express.json()); //declare this to receive json objects.

Other answers failed to mention, when making the request to express via fetch or other clients. The request must be formatted a certain way.

const response = await fetch(`${expressAddress}/controller/route`, {       method: 'POST', // *GET, POST, PUT, DELETE, etc.      headers: {'Content-Type': 'application/json' //this must be set to a json type      },      body: JSON.stringify(row) //regular js object need to be converted to json  })

If you make the fetch request like this the req.body will output your json object as expected.


Viewing all articles
Browse latest Browse all 54

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>