Using third-party middleware
There are a lot of third-party middleware functions that you can use in your Express applications. Let’s see how to install and use them.
One of the most popular middleware functions is body-parser
(https://www.npmjs.com/package/body-parser). Basically, it will parse the HTTP body of the incoming request and make it available under the req.body
property.
Install it using npm
as follows:
npm install body-parser@1
Then you can import it and use it in your application. Create a new file called echo_payload.js
with the following content:
import express from 'express' import bodyParser from 'body-parser' const app = express() const port = 3000 app.use(bodyParser.json()) app.post('/echo', (req, res) => { // Echo the request body res.json(req.body) }) app.listen(port, () => { console.log(`running at http://localhost:${port}`) })
Now run the application...