Express.js express.raw() Middleware

Beginner
5 min read
Updated: Nov 2026
3 examples
express()

Definition

In Express.js, express.raw() is built-in middleware that reads the request body into a Buffer on req.body. By default it matches Content-Type: application/octet-stream.

Use it for webhooks, signatures, or any binary/raw payload you must not parse as JSON. Mount it with app.use() before routes that need the buffer.

Syntax

Call express.raw in your Express.js app. Start with this line.

Express
express.raw([options])
CallWhat it does
express.raw([options])Returns middleware that sets req.body to a Buffer.

Install with npm install express. Create the app with const app = express() before mounting parsers.

Parameters

ParameterDescription
optionsOptional. Parsing settings. Omit for defaults.
options.limitOptional. Max body size. Default '100kb'.
options.typeOptional. Which Content-Type to parse. Default 'application/octet-stream'.
options.inflateOptional. Default true: inflate gzip/deflate bodies.
options.verifyOptional. verify(req, res, buf, encoding) — inspect the buffer; throw to abort.

Return Value

express.raw() returns a middleware function. Mount it with app.use(express.raw()). After it runs, req.body is a Buffer (or undefined if the type did not match).

Express
const rawParser = express.raw({ type: 'application/octet-stream' });
app.use(rawParser);

Express.js Examples

Example — Read a Raw Buffer Body

Mount the parser and report the byte length.

Express
const express = require('express');
const app = express();

app.use(express.raw());

app.post('/upload', (req, res) => {
  const buf = req.body;
  res.json({
    isBuffer: Buffer.isBuffer(buf),
    bytes: buf ? buf.length : 0
  });
});

app.listen(3000);

How It Works

  1. Mount early. app.use(express.raw()) runs before routes.
  2. Match the type. Default type is application/octet-stream.
  3. Fill req.body. The value is a Node.js Buffer.
  4. Respond. Report length without parsing as text or JSON.

Example — Parse JSON Content-Type as Raw Bytes

Keep the original bytes for HMAC checks while the client still sends JSON.

Express
const express = require('express');
const app = express();

app.use(express.raw({ type: 'application/json' }));

app.post('/webhook', (req, res) => {
  const raw = req.body.toString('utf8');
  res.json({ raw: raw });
});

app.listen(3000);

How It Works

  1. Override type. type: 'application/json' still stores a Buffer.
  2. No JSON.parse yet. You control when (or if) to parse.
  3. Stringify for logs. toString('utf8') shows the original text.
  4. Use carefully. Do not also mount express.json() on the same path if you need the raw bytes.

Example — Cap Payload Size

Reject oversized binary uploads early.

Express
const express = require('express');
const app = express();

app.use(express.raw({ limit: '50kb' }));

app.post('/bin', (req, res) => {
  res.sendStatus(204);
});

app.listen(3000);

How It Works

  1. Pass limit. { limit: '50kb' } caps the body.
  2. Small bodies succeed. Handler runs and returns 204.
  3. Large bodies fail. Parser stops before your route.
  4. Keep limits modest. Default is already 100kb.

Runtime Support

express.raw() is Express.js built-in middleware. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it.

✓ Server · Express 4.17+ & 5

express.raw()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
express.raw() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It is middleware that reads matching request bodies into a Buffer on req.body. Default Content-Type is application/octet-stream.
Use it for binary payloads or when you need the exact original bytes (for example webhook signature checks).
The Content-Type did not match the type option, the parser was mounted after the route, or there was no body.
Yes. Pass { type: 'application/json' }. Do not also mount express.json() on the same path if you need the raw bytes.
No. It runs on your Node.js server. Browsers only send the HTTP request.

Next: express.Router()

Create modular route stacks with the Express Router factory.

express.Router() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful