Express.js express.text() Function
Last Updated :
15 Jul, 2025
The express.text() function is a built-in middleware in Express.js that parses incoming HTTP request bodies with a text/plain content type. It allows you to easily handle raw text data sent in the body of a request, making it suitable for handling non-JSON, non-URL-encoded, or non-multipart data.
The middleware parses the request body and makes it available in req.body, allowing you to access and work with the text data easily.
Syntax
express.text([options])]
Parameter: The options parameter contains various properties like defaultCharset, inflate, limit, verify, etc.
- defaultCharset: Specifies the default character encoding (default: utf-8)
- inflate: Allows handling of compressed request bodies
- limit: Defines the maximum size of the text payload
- verify: Custom function to validate request body before parsing
Return Value: Returns an Object containing the parsed text data.
Steps to Use express.text() Middleware
Step 1: Set Up a Node.js Project
mkdir express-text-demo
cd express-text-demo
npm init -y
Step 2: Install Express
npm install express
Step 3: Create index.js and Add the Middleware
This example demonstrates how to use express.text() to parse incoming plain text requests
JavaScript
// Filename - index.js
const express = require("express");
const app = express();
const PORT = 3000;
// Middleware to parse text/plain content
app.use(express.text());
app.post("/", (req, res) => {
console.log("Received Text Data:", req.body);
res.send(`Server received: ${req.body}`);
});
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`);
});
- app.use(express.text()); enables Express to automatically parse incoming text/plain data and convert it into a string
- When a POST request is made to /, the server logs the received text (req.body) and sends a response confirming the received data
- app.listen(PORT, ...) starts the server and logs a message indicating that it is running on port 3000
To start the application run the following command.
node index.js
Testing the API
1. Send a POST request with the header Content-Type: text/plain and the body
You can use POSTMAN or ThunderClient and click on the headers in it set the header as Content-Type: text/plain and then click on the body option in it then on text and add some text to it.
API Headers2. Fill up the body and make the POST request
Write the text you want to send over the network in the body section. Now click on the dropdown at the left and select POST request from it and the click on the send button.
Express.js express.text() FunctionAs soon as you send the text on to the server server will respond with the same text message to you after parsing it.
How Does express.text() Work?
When you set up express.text() as middleware in your Express app, it processes requests where the Content-Type header is set to text/plain. The middleware reads the body of the request and transforms it into a plain text string, which can then be accessed via req.body.
- The client sends an HTTP request with a Content-Type of text/plain.
- The request body is passed to the express.text() middleware.
- The middleware processes the body and adds the text content to req.body.
- You can access the plain text data directly from req.body in your route handler.
The Size limit parameter present in options
To prevent large requests from overloading the server, you can set a size limit for text payloads using the limit option
- app.use(express.text({ limit: "100b" })); ensures that incoming text data does not exceed 100 bytes, preventing large payloads.
- When a POST request is made to /, the server logs the received text (req.body) and sends a response confirming the received data
- app.listen(PORT, ...) runs the server and logs a message indicating it is running on port 3000
- If the text payload exceeds 100 bytes, the server returns a 413 Payload Too Large error.
What Happens Without express.text()?
If the express.text() middleware is not used, req.body will be undefined, making it impossible to read text-based payloads
JavaScript
const express = require("express");
const app = express();
const PORT = 3000;
app.post("/", (req, res) => {
console.log("Received Text Data:", req.body); // req.body will be undefined
res.end();
});
app.listen(PORT, () => {
console.log(`Server running on PORT ${PORT}`);
});
Output
Output without express.text() middleware- When a POST request is made to /, the server tries to log the received text data (req.body)
- Since no middleware (like express.text() or express.json()) is used to parse the request body, req.body will be undefined
- app.listen(PORT, ...) starts the server and logs a message indicating it is running on port 3000, but the POST request won’t work properly without a body-parsing middleware
Common Use cases of express.text() middleware
- Handling Chat Messages: Used in chat applications where messages are sent as plain text instead of JSON
- Processing Webhooks: Some external services send webhook data as plain text, which express.text() helps parse
- Logging and Monitoring: Collects log messages or status updates from clients and stores them for analysis
- Text-Based APIs: Useful in APIs where requests contain plain text input, such as search queries or commands
- Handling File Metadata: Some file upload services send metadata as plain text, which can be parsed using express.text()
Conclusion
The express.text() middleware in Express.js is essential for handling plain text requests by converting them into a string accessible via req.body. It is useful in scenarios like chat applications, webhook processing, logging, and APIs that require text-based input. By using express.text(), developers can efficiently process text data, improve request handling, and ensure structured text parsing in their applications
Similar Reads
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read
HTML Tutorial
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
JS Tutorial
JavaScript TutorialJavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.Client Side: On the client side, JavaScript works
11 min read
JSON TutorialJSON (JavaScript Object Notation) is a widely-used, lightweight data format for representing structured data. Used Extensively : Used in APIs, configuration files, and data exchange between servers and clients.Text-based: JSON is a simple text format, making it lightweight and easy to transmit.Human
5 min read
TypeScript TutorialTypeScript is a superset of JavaScript that adds extra features like static typing, interfaces, enums, and more. Essentially, TypeScript is JavaScript with additional syntax for defining types, making it a powerful tool for building scalable and maintainable applications.Static typing allows you to
8 min read
Vue.js TutorialVue.js is a progressive JavaScript framework for building user interfaces. It stands out for its simplicity, seamless integration with other libraries, and reactive data binding.Built on JavaScript for flexible and component-based development.Supports declarative rendering, reactivity, and two-way d
4 min read
jQuery TutorialjQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
Front End
React TutorialReact is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
Angular TutorialAngular is a powerful, open-source web application framework for building dynamic and scalable single-page applications (SPAs). Developed by Google, Angular provides a comprehensive solution for front-end development with tools for routing, form handling, HTTP services, and more.Designed for buildin
4 min read
Backend
Node.js TutorialNode.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme
4 min read
Express.js TutorialExpress.js is a minimal and flexible Node.js web application framework that provides a list of features for building web and mobile applications easily. It simplifies the development of server-side applications by offering an easy-to-use API for routing, middleware, and HTTP utilities.Built on Node.
4 min read
PHP TutorialPHP is a popular, open-source scripting language mainly used in web development. It runs on the server side and generates dynamic content that is displayed on a web application. PHP is easy to embed in HTML, and it allows developers to create interactive web pages and handle tasks like database mana
9 min read
Laravel TutorialLaravel is an open-source PHP web application framework that has gained immense popularity since its inception in 2011, created by Taylor Otwell. This renowned framework empowers developers to build robust, scalable web applications with remarkable ease. As a developer-friendly framework, Laravel of
3 min read
Database
Web Technologies Questions The following Web Technologies Questions section contains a wide collection of web-based questions. These questions are categorized based on the topics HTML, CSS, JavaScript, and many more. Each section contains a bulk of questions with multiple solutions. Table of Content HTML QuestionsCSS Question
15+ min read