express.text() is built-in middleware that parses request bodies sent with
Content-Type: text/plain and exposes them as a plain string on req.body.
Syntax
express.text([options])
options includes defaultCharset, inflate, limit, and verify.
Example
const express = require("express");
const app = express();
const PORT = 4500;
app.use(express.text());
app.post("/notes", (req, res) => {
console.log("Received note:", req.body);
res.send(`Saved note: ${req.body}`);
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
A POST request with Content-Type: text/plain and a plain-text body echoes that text back.
Using the limit option
app.use(express.text({ limit: "50b" }));
app.post("/notes", (req, res) => {
res.send(req.body);
});
Bodies larger than 50 bytes get rejected with a 413 Payload Too Large error.
Without the middleware
const express = require("express");
const app = express();
app.post("/notes", (req, res) => {
console.log(req.body); // undefined
res.end();
});
Common use cases
- Chat or messaging systems that send raw text instead of JSON.
- Webhooks from services that deliver plain-text payloads.
- Simple logging endpoints.
- Text-based search queries or commands.
- Parsing plain-text metadata sent by upload clients.