node middleware: body-parser

2021-11-06

 | 

~1 min read

 | 

93 words

body-parser is middleware for express servers in Node. It will automatically parse the data in a response or request and can be “used” by the server as desired.

This is very convenient!

For example, using the json method the following are equivalent:

let bodyRaw = ""
app.post("/foo", (req, res) => {
  req.on("data", (chunk) => {
    bodyRaw += chunk
  })
  const body = JSON.parse(bodyRaw)
})
import { json } from "body-parser"
app.use(json())

app.post("/foo", (req, res) => {
  const body = req.body
})

Documentation can be found on the express site.



Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!