Q10: What is the purpose of `express.json()` and `express.urlencoded()`?

Difficulty:EasyTopic:Parsing Middleware

Both are built-in body parsing middleware functions introduced in Express 4.16+ (re-integrated from body-parser).

  • express.json(): Parses incoming HTTP requests carrying application/json payload headers and populates req.body with a JavaScript object.
  • express.urlencoded({ extended: true }): Parses incoming request payloads carrying application/x-www-form-urlencoded headers (standard HTML forms). The extended: true option uses the qs library allowing nested object parsing.
Key Takeaways
  • Body parsers read raw incoming stream buffers and format them into req.body.
  • extended: true permits parsing deeply nested form payloads.