How to consume the JSON POST data in an Express application?

Sometimes, we want to consume the JSON POST data in an Express application.

In this article, we’ll look at how to consume the JSON POST data in an Express application.

How to consume the JSON POST data in an Express application?

To consume the JSON POST data in an Express application, we call app.post.

For instance, we write

const express = require('express');

const app = express();

app.use(express.json());

app.post('/', (request, response) => {
  console.log(request.body);
  response.send(request.body);
});

app.listen(3000);

to call app.post with the route URL and the route handler callback.

In the callback, we get the request body from request.body.

request.body is available since we use the middleware returned by express.json with

app.use(express.json());

Conclusion

To consume the JSON POST data in an Express application, we call app.post.