Middleware for parsing raw request bodies as Buffer objects. Parses all bodies as binary data and only processes requests where the Content-Type header matches the type option.
Creates middleware that parses raw bodies and populates req.body with a Buffer containing the raw request data.
/**
* Create middleware to parse raw bodies as Buffer
* @param {object} [options] - Configuration options
* @returns {function} Express middleware function
*/
function raw(options?: {
inflate?: boolean;
limit?: number | string;
type?: string | string[] | ((req: IncomingMessage) => boolean);
verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;booleantruetrue, deflated (compressed) bodies will be inflated; when false, deflated bodies are rejectednumber | string'100kb'string | string[] | function'application/octet-stream'functionundefinedverify(req, res, buf, encoding) where buf is the raw request body Buffer. Can abort parsing by throwing an errorBasic raw parsing:
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
// Parse raw bodies with default options
app.use(bodyParser.raw());
app.post('/upload', (req, res) => {
console.log(req.body); // <Buffer 89 50 4e 47 0d 0a 1a 0a ...>
console.log(req.body.length); // Size in bytes
res.send(`Received ${req.body.length} bytes`);
});File upload handling:
app.use(bodyParser.raw({
limit: '50mb',
type: 'image/*'
}));
app.post('/image', (req, res) => {
const imageBuffer = req.body;
// Save or process the image buffer
require('fs').writeFile('uploaded-image.png', imageBuffer, (err) => {
if (err) throw err;
res.send('Image saved');
});
});Binary data processing:
app.use(bodyParser.raw({
type: 'application/octet-stream',
limit: '10mb'
}));
app.post('/binary', (req, res) => {
const buffer = req.body;
// Process binary data
const header = buffer.slice(0, 4);
const data = buffer.slice(4);
console.log('Header:', header.toString('hex'));
console.log('Data length:', data.length);
res.json({
headerHex: header.toString('hex'),
dataLength: data.length
});
});Custom type matching:
app.use(bodyParser.raw({
type: (req) => {
// Accept any binary content type
return req.headers['content-type'] &&
(req.headers['content-type'].startsWith('application/') ||
req.headers['content-type'].startsWith('image/') ||
req.headers['content-type'].startsWith('video/'));
}
}));With verification:
app.use(bodyParser.raw({
verify: (req, res, buf, encoding) => {
// Verify the buffer contains expected data
if (buf.length === 0) {
throw new Error('Empty buffer not allowed');
}
// Check for specific file signatures
const pngSignature = Buffer.from([0x89, 0x50, 0x4E, 0x47]);
if (!buf.slice(0, 4).equals(pngSignature)) {
throw new Error('Invalid PNG signature');
}
},
type: 'image/png'
}));Processing with streams:
const crypto = require('crypto');
app.use(bodyParser.raw({
limit: '100mb',
verify: (req, res, buf, encoding) => {
// Calculate hash while verifying
const hash = crypto.createHash('sha256');
hash.update(buf);
req.fileHash = hash.digest('hex');
}
}));
app.post('/secure-upload', (req, res) => {
const buffer = req.body;
const hash = req.fileHash;
console.log(`Received ${buffer.length} bytes with hash: ${hash}`);
res.json({ size: buffer.length, hash });
});The raw parser can throw several types of errors:
app.use(bodyParser.raw());
app.use((err, req, res, next) => {
if (err.type === 'entity.too.large') {
return res.status(413).send('File too large');
}
if (err.type === 'encoding.unsupported') {
return res.status(415).send('Unsupported content encoding');
}
next(err);
});