diff --git a/src/middlewares/RawBody.ts b/src/middlewares/RawBody.ts new file mode 100644 index 0000000..58ff67d --- /dev/null +++ b/src/middlewares/RawBody.ts @@ -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 \ No newline at end of file