Express.js express.text() Middleware

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

Definition

In Express.js, express.text() is built-in middleware that reads a text body and stores a string on req.body. By default it matches Content-Type: text/plain.

Use it when clients send plain text — notes, CSV snippets, or simple messages — not JSON objects. Mount it with app.use() before routes that read the string.

Syntax

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

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

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 'text/plain'.
options.defaultCharsetOptional. Charset when the request omits one. Default 'utf-8'.
options.inflateOptional. Default true: inflate gzip/deflate bodies.
options.verifyOptional. verify(req, res, buf, encoding) — inspect the raw buffer; throw to abort.

Return Value

express.text() returns a middleware function. After it runs, req.body is a string (or undefined if the type did not match).

Express
app.use(express.text({ limit: '20kb' }));

Express.js Examples

Example — Parse a Plain Text Body

Echo the string the client sent.

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

app.use(express.text());

app.post('/note', (req, res) => {
  res.type('text').send('You wrote: ' + req.body);
});

app.listen(3000);

How It Works

  1. Mount the parser. express.text() runs first.
  2. Match text/plain. Other types are skipped by default.
  3. Fill req.body. The value is a JavaScript string.
  4. Respond. Echo the text back to the client.

Example — Reject Empty Text

Treat blank strings as bad input.

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

app.use(express.text());

app.post('/message', (req, res) => {
  const text = typeof req.body === 'string' ? req.body.trim() : '';
  if (!text) {
    return res.status(400).send('message required');
  }
  res.status(201).send('saved');
});

app.listen(3000);

How It Works

  1. Trim the string. Whitespace-only bodies count as empty.
  2. Return 400. Client gets a clear error.
  3. Happy path. Non-empty text returns 201.
  4. Keep it simple. No JSON schema — just a string check.

Example — Cap Text Size

Keep memory use low for open endpoints.

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

app.use(express.text({ limit: '10kb' }));

app.post('/paste', (req, res) => {
  res.json({ length: req.body.length });
});

app.listen(3000);

How It Works

  1. Set limit. { limit: '10kb' } rejects bigger bodies.
  2. Small text works. Route receives the string.
  3. Large text fails. Parser errors before your handler.
  4. Default is 100kb. Tighten it for public paste endpoints.

Runtime Support

express.text() 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.text()

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.text() 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 text bodies into a string on req.body. Default Content-Type is text/plain.
express.json() parses JSON into objects or arrays. express.text() leaves the body as a plain string.
The Content-Type did not match text/plain (or your type option), or the parser was mounted after the route.
It uses the charset from Content-Type when present. Otherwise defaultCharset is utf-8.
No. It runs on your Node.js server. Browsers only send the HTTP request.

Next: express.urlencoded()

Parse HTML form posts into req.body objects.

express.urlencoded() 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