23 lines
564 B
TypeScript
23 lines
564 B
TypeScript
import { Request, Response } from 'express';
|
|
|
|
/**
|
|
* Custom express middleware that appends the raw body to the request obeject.
|
|
* Mainly used for parsing csvs from boddies.
|
|
*/
|
|
|
|
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
|
|
next()
|
|
})
|
|
req.on('error', () => {
|
|
res.sendStatus(400)
|
|
})
|
|
}
|
|
|
|
export default RawBodyMiddleware |