or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdjson-parser.mdraw-parser.mdtext-parser.mdurlencoded-parser.md
tile.json

text-parser.mddocs/

Text Parser

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.

Capabilities

Text Parser Factory

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;

Options

defaultCharset

  • Type: string
  • Default: 'utf-8'
  • Description: Default character set for text content if charset is not specified in the Content-Type header

inflate

  • Type: boolean
  • Default: true
  • Description: When true, deflated (compressed) bodies will be inflated; when false, deflated bodies are rejected

limit

  • Type: number | string
  • Default: '100kb'
  • Description: Controls the maximum request body size. If a number, specifies bytes; if a string, passed to the bytes library for parsing

type

  • Type: string | string[] | function
  • Default: 'text/plain'
  • Description: Determines what media type the middleware will parse. Can be an extension name, MIME type, MIME type with wildcard, or function

verify

  • Type: function
  • Default: undefined
  • Description: Called as verify(req, res, buf, encoding) where buf is the raw request body Buffer. Can abort parsing by throwing an error

Usage Examples

Basic 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');
  }
}));

Use Cases

Content Management

  • Process plain text content for blogs or documentation
  • Handle markdown or other markup languages
  • Process configuration files as text

Data Import

  • Parse CSV files for data import
  • Process XML feeds or configuration
  • Handle log files and other structured text

Legacy Systems

  • Interface with systems that use plain text protocols
  • Handle specific character encodings
  • Process custom text-based data formats

Character Encoding

The text parser supports various character encodings through the iconv-lite library:

  • UTF-8 (default): Universal encoding for most text
  • ISO-8859-1: Latin-1 encoding for Western European languages
  • ASCII: Basic ASCII text
  • And many others: Any encoding supported by iconv-lite

Encoding is determined by:

  1. Content-Type header charset parameter
  2. defaultCharset option if no charset in header
  3. Falls back to 'utf-8' if neither is specified

Error Handling

The text parser can throw several types of errors:

  • 400 Bad Request: General parsing error
  • 413 Payload Too Large: Body exceeds size limit
  • 415 Unsupported Media Type: Unsupported character encoding
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);
});