express.raw() is built-in middleware that reads an incoming request body into a Buffer
rather than parsing it as JSON or text. It's the go-to choice when the payload is raw binary
data.
Syntax
express.raw([options])
options supports properties like inflate, limit, and type.
With the middleware
const express = require("express");
const app = express();
const PORT = 4500;
app.use(express.raw());
app.post("/upload-cover", (req, res) => {
console.log(req.body); // a Buffer
res.send(`Received ${req.body.length} bytes`);
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
Sending a POST request with Content-Type: application/octet-stream and a binary body
results in req.body being a Buffer containing the raw bytes.
Without the middleware
const express = require("express");
const app = express();
app.post("/upload-cover", (req, res) => {
console.log(req.body); // undefined
res.end();
});
Without express.raw(), Express has no idea how to interpret the body, so req.body stays
undefined.
Use cases
- Accepting file uploads sent as raw bytes.
- Handling webhook payloads that require verifying the raw body (e.g. signature checks).
- Working with binary protocols instead of JSON or form data.