Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *