The goal of this tutorial is to explain how to integrate third-party middleware into your Express.js application with body-parser as an example. By the end of this tutorial, you will be able to:
Prerequisites:
- Basic understanding of Node.js and Express.js
- Node.js and npm (Node Package Manager) installed on your machine
Middleware is software that lies in the middle between the operating system and the applications on it. It provides services to applications outside what’s offered by the operating system.
Express.js middleware are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.
body-parser is a piece of express middleware that reads a form's input and stores it as a javascript object accessible through req.body
.
npm install body-parser
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
The json()
and urlencoded()
are built-in middleware functions in Express. They parse incoming requests with JSON payloads and URL-encoded payloads, respectively.
Here is an example of using body-parser in an Express.js application:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// parse application/json
app.use(bodyParser.json());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/api/data', (req, res) => {
// Access data from the request body here
console.log(req.body);
res.send('Data received!');
});
app.listen(3000, () => console.log('Server running on port 3000'));
In this example, we first import Express and body-parser. We then tell our Express app to use the middleware. Finally, we define a POST route that logs the request body to the console.
In this tutorial, we have:
To continue your learning journey, you might want to look into other popular Express middleware such as cookie-parser and morgan.
Exercise 1: Create an Express application that uses body-parser to parse incoming JSON payloads.
Exercise 2: Create a form on the front end and send a POST request with the form data to your Express application. Parse the incoming request using body-parser.
Exercise 3: Use body-parser to parse incoming URL-encoded payloads in your Express application.
Solutions and explanations:
This exercise is straightforward if you followed the tutorial. You need to create an Express application, integrate body-parser, and parse incoming JSON payloads.
In this exercise, you need to create a form on the front end (you can use simple HTML for this) and send a POST request with the form data to your Express application. You will then use body-parser to parse the incoming request.
To parse URL-encoded payloads, you will need to use the bodyParser.urlencoded({ extended: true })
method. The 'extended' syntax allows for rich objects and arrays to be encoded into the URL-encoded format, allowing for a JSON-like experience with URL-encoded.