Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Express.js · Function

express.text

express.text([options]) creates middleware that parses matching request bodies into strings and exposes them as req.body. It supports configurable media types, size limits, character sets, and compressed request bodies.

Since
4.17.0

How it works

Calling express.text(options) creates middleware that examines the request Content-Type, parses matching bodies into strings, and places the result on req.body. It accepts Unicode text and can automatically decompress gzip or deflate bodies before parsing.

Syntax

express.text([options])

Parameters

NameTypeDescription
optionsObject | undefinedOptional configuration object controlling text parsing.
options.defaultCharsetStringFallback character set when Content-Type omits charset; default is utf-8.
options.inflateBooleanWhether to handle compressed bodies; default is true.
options.limitNumber | StringMaximum request body size; a number means bytes, while a string is parsed by the bytes library.
options.typeString | String[] | FunctionMedia types to parse, or a function fn(req) returning a truthy value; default is text/plain.

Returns

Middleware that parses matching request bodies as strings and assigns the parsed value to req.body.

Key points

  • The middleware converts an eligible request body into a JavaScript string.
  • The function is a middleware factory: invoke it first, then register the returned middleware with app.use or a route.
  • Text can use any Unicode encoding supported by the parser.
  • Compressed gzip and deflate request bodies are decompressed automatically by default.
  • If Content-Type omits a charset, the default character set is UTF-8.

Common mistakes

  • Requests whose media type does not match type are not parsed, so req.body will not contain the submitted text.
  • Setting inflate to false rejects compressed bodies instead of decompressing them.
  • Do not assume req.body is always a string; unmatched, empty, or failed parsing can leave it as an empty object.

Example: Parse and echo plain text

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

app.use(express.text({ type: 'text/plain', limit: '10kb' }));

app.post('/echo', (req, res) => {
  res.type('text/plain').send(`Received: ${req.body}`);
});

app.listen(3000);

This application parses matching plain-text request bodies into req.body and echoes the resulting string.

When to use it

Receive plain-text webhooks

Webhook endpoints that receive signed or structured text payloads.

Echo or transform text

Small services that echo or transform submitted text.

Parse custom media types

Endpoints accepting custom text-based media types.

Interview questions

QWhat does express.text() do?

express.text() returns middleware that parses matching request payloads as strings. It checks the request Content-Type against the type option and stores the parsed value in req.body.

Follow-up: How would you accept a custom media type? Pass type as a string such as application/custom, an array, or a function that receives req and returns a truthy value.

QWhat are the important defaults?

The default type is text/plain, and the default maximum body size is 100kb. The limit may be a byte count or a size string interpreted by the bytes library.

Follow-up: What happens when a request exceeds the limit? The parser rejects the request rather than producing a parsed body.

QHow does express.text() handle encoding and compression?

The parser accepts any Unicode encoding and automatically inflates gzip and deflate content by default. Setting inflate to false causes deflated bodies to be rejected.

Follow-up: How is a missing charset handled? defaultCharset supplies the fallback character set, defaulting to utf-8.

  • express.json
  • express.raw
  • express.urlencoded
  • req.body
  • body-parser

Source: Express Object · Express.js 4.x. Checked 2026-10-15.