The body-parser module in Express.js is used to parse incoming request bodies before your handlers, available under the req.body property. It is typically used to parse form data, but can also be used to parse JSON data from POST requests.

Example:

const express = require(‘express’);
const bodyParser = require(‘body-parser’);

const app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

// POST method route
app.post(‘/’, function (req, res) {
res.send(req.body);
});

app.listen(3000);

Leave a Reply

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