Added rawbody if needed

ref #22
This commit is contained in:
Nicolai Ort 2020-12-16 19:00:25 +01:00
parent 1b1f8f2b09
commit b9fd2379f4

View File

@ -0,0 +1,27 @@
import { Request, Response } from 'express';
const RawBodyMiddleware = (req: Request, res: Response, next: () => void) => {
const body = []
req.on('data', chunk => {
body.push(chunk)
})
req.on('end', () => {
const rawBody = Buffer.concat(body)
req['rawBody'] = rawBody
/*
switch (req.header('content-type')) {
case 'application/json':
req.body = JSON.parse(rawBody.toString())
break
// add more body parsing if needs be
default:
}
*/
next()
})
req.on('error', () => {
res.sendStatus(400)
})
}
export default RawBodyMiddleware