How to get the parsed body and the original body at the same time in Express

Learn how to get the parsed body and the original body at the same time in Expressbody-parser

I ran into this problem in an application I was building.

Using Express, I can importbody-parserParse the body as JSON:

import bodyParser from 'body-parser'
app.use(bodyParser.json())

However, to integrate with the Stripe Payments API, I need to exposePrimitive body(Unparsed) into the endpoint, and I still cannot parse the body as JSON, but I don't know how to do it.

This achieves the goal:

app.use(bodyParser.json({
  verify: (req, res, buf) => {
    req.rawBody = buf
  }
}))

Now available atreq.rawBodyAnd the JSON parsed data can be usedreq.body.

Frombody-parserOn GitHub, I found that this doubles the RAM usage per request, but since I need this feature, I have no choice.

In addition to possibly just creating other servers for Stripe Webhooks that I want to handle.

Download mine for freeNode.js manual


More node tutorials: