- Import the necessary modules:
const express = require('express');
const bodyParser = require('body-parser');
- Create an Express app:
const app = express();
- Use bodyParser middleware to parse the request body:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
- Define a route that handles incoming POST requests:
app.post('/example-route', (req, res) => {
// Access the request body data
const requestBody = req.body;
// Perform necessary operations with the request body data
// Send a response
res.send('Request body received');
});
- Start the server and listen on a specific port:
const PORT = 3000; // Choose a port number
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});