

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: HTTP Clients
incomplete
2: JSON
incomplete
3: JSON Middleware
incomplete
4: The Profane
incomplete
This lesson's interactive features are locked, please to keep using them
Manually parsing the request body with streams is pretty tedious. Luckily, Express has convenient built-in middleware to parse JSON request bodies. All you need is:
import express from "express";
const app = express();
// Built-in JSON body parsing middleware
app.use(express.json());
Express will automatically:
Content-Type header is set to application/jsonreq.body.async function handler(req: Request, res: Response) {
type parameters = {
body: string;
};
// req.body is automatically parsed
const params: parameters = req.body;
// ...
}
Let's add JSON middleware to our server.
You can re-submit the previous lesson to test your refactored code.