Middleware for parsing text request bodies as strings. Parses all bodies as plain text and only processes requests where the Content-Type header matches the type option.
Creates middleware that parses text bodies and populates req.body with a string containing the decoded text content.
/**
* Create middleware to parse text bodies as strings
* @param {object} [options] - Configuration options
* @returns {function} Express middleware function
*/
function text(options?: {
defaultCharset?: string;
inflate?: boolean;
limit?: number | string;
type?: string | string[] | ((req: IncomingMessage) => boolean);
verify?: (req: IncomingMessage, res: ServerResponse, buf: Buffer, encoding: string) => void;
}): (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;string'utf-8'booleantruetrue, deflated (compressed) bodies will be inflated; when false, deflated bodies are rejectednumber | string'100kb'string | string[] | function'text/plain'functionundefinedverify(req, res, buf, encoding) where buf is the raw request body Buffer. Can abort parsing by throwing an errorBasic text parsing:
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
// Parse text with default options
app.use(bodyParser.text());
app.post('/message', (req, res) => {
console.log(req.body); // "Hello, this is plain text content"
console.log(typeof req.body); // "string"
res.send(`Received: ${req.body.substring(0, 50)}...`);
});CSV data processing:
app.use(bodyParser.text({
type: 'text/csv',
limit: '1mb'
}));
app.post('/csv', (req, res) => {
const csvData = req.body;
const lines = csvData.split('\n');
const headers = lines[0].split(',');
console.log('CSV headers:', headers);
console.log('Row count:', lines.length - 1);
res.json({
headers,
rowCount: lines.length - 1
});
});XML processing:
app.use(bodyParser.text({
type: 'application/xml',
defaultCharset: 'utf-8'
}));
app.post('/xml', (req, res) => {
const xmlData = req.body;
// Process XML (would typically use an XML parser)
console.log('Received XML:', xmlData.substring(0, 100));
// Simple tag extraction example
const titleMatch = xmlData.match(/<title>(.*?)<\/title>/);
const title = titleMatch ? titleMatch[1] : 'No title found';
res.json({ title, length: xmlData.length });
});Multiple text types:
app.use(bodyParser.text({
type: ['text/*', 'application/xml', 'application/rss+xml']
}));
app.post('/content', (req, res) => {
const contentType = req.headers['content-type'];
const text = req.body;
console.log(`Processing ${contentType}: ${text.length} characters`);
res.json({
contentType,
characterCount: text.length,
wordCount: text.split(/\s+/).length
});
});Custom charset handling:
app.use(bodyParser.text({
defaultCharset: 'iso-8859-1',
type: 'text/plain'
}));
app.post('/legacy-text', (req, res) => {
// Handle legacy text with specific encoding
const text = req.body;
console.log('Legacy text received:', text);
res.send('Text processed');
});With verification:
app.use(bodyParser.text({
verify: (req, res, buf, encoding) => {
// Verify text contains only valid characters
const text = buf.toString(encoding || 'utf-8');
if (text.includes('\0')) {
throw new Error('Null characters not allowed');
}
if (text.length === 0) {
throw new Error('Empty text not allowed');
}
}
}));Log processing:
app.use(bodyParser.text({
type: 'text/plain',
limit: '10mb'
}));
app.post('/logs', (req, res) => {
const logData = req.body;
const lines = logData.split('\n').filter(line => line.trim());
// Process log entries
const errorLines = lines.filter(line => line.includes('ERROR'));
const warningLines = lines.filter(line => line.includes('WARN'));
res.json({
totalLines: lines.length,
errors: errorLines.length,
warnings: warningLines.length,
sample: lines.slice(0, 5)
});
});Custom type function:
app.use(bodyParser.text({
type: (req) => {
// Parse any text-based content
const contentType = req.headers['content-type'] || '';
return contentType.startsWith('text/') ||
contentType.includes('xml') ||
contentType.includes('csv');
}
}));The text parser supports various character encodings through the iconv-lite library:
Encoding is determined by:
defaultCharset option if no charset in headerThe text parser can throw several types of errors:
app.use(bodyParser.text());
app.use((err, req, res, next) => {
if (err.type === 'entity.too.large') {
return res.status(413).send('Text too large');
}
if (err.type === 'encoding.unsupported') {
return res.status(415).send('Unsupported text encoding');
}
if (err.type === 'charset.unsupported') {
return res.status(415).send('Unsupported charset');
}
next(err);
});