req.body is undefined or empty. The express engine does not read the body of the incoming request that is
If we want to read the request body and parse it into
req.body, then you need to install the appropriate middleware.
First, it will see what type of request it is and does it have a body (like a POST or a PUT). Second, it will check the incoming
content-type and does it know how to parse that.
If both matches then the middleware parses it and put it into
req.body. If we do not have this type of middleware installed, then
req.body will be undefined or empty.
Express has several built-in middleware for content-types. We can simply use them inside our express app. The following are some middleware for different content types:
express.json(...) for "application/json" express.raw(...) reads the body into a Buffer for you to parse yourself express.text(...) for "text/plain" - reads the body into a string express.urlencoded(...) for "application/x-www-form-urlencoded"
If the incoming request’s
content-type is JSON, we can then use the following code inside the express app.