🚀 Express.js
Beginner
What is express.json() middleware?
Answer
express.json() is a built-in Express middleware that parses incoming requests with Content-Type: application/json and populates req.body with the parsed JavaScript object. It is the standard way to accept JSON data from API clients. Register it globally with app.use(express.json()) before your route definitions. If the JSON is malformed, the middleware automatically responds with a 400 error. You can configure options like limit to restrict request body size: express.json({ limit: '1mb' }). Before Express 4.16, this functionality required a separate body-parser package — it is now built in.