Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

als-body-parser

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

als-body-parser

A flexible, efficient body parsing middleware for Node.js, supporting JSON, URL-encoded, and plain text, with customizable options for request handling.

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

als-body-parser

als-body-parser is a flexible middleware for parsing incoming request bodies in a Node.js environment, making it easy to integrate with frameworks like Express or with the native HTTP server. It adds a req.body property to the incoming request object, which can be a JSON object, URL-encoded string, or plain text, depending on the Content-Type of the request.

Installation

Install using npm:

npm install als-body-parser

Basic Usage

With Native HTTP Server

const http = require('http');
const bodyParser = require('als-body-parser')({});

// Create HTTP server and use bodyParser middleware
http.createServer((req, res) => {
  bodyParser(req, res, () => {
    if (req.body) {
      // Send parsed body back as JSON
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify(req.body));
    } else {
      // Handle cases where no body could be parsed
      res.writeHead(400);
      res.end('No body parsed');
    }
  });
}).listen(3000);

With Express

const express = require('express');
const bodyParser = require('als-body-parser')({});

const app = express();
app.use(bodyParser);

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

app.listen(3000);

Advanced Usage

Configuration Options

als-body-parser can be customized with several options:

  • supportedMethods: Array of HTTP methods to parse (default: ['POST', 'PUT', 'PATCH']).
  • supportedCt: Array of supported content types (default: ['application/x-www-form-urlencoded', 'application/json', 'text/plain']).
  • limit: Maximum allowed size of the request body in bytes (default: 1048576).
  • timeout: Timeout in milliseconds for receiving the request body (default: 5000).
  • logger: Function to log errors (default: console.log).
  • httpErrorHandler: Function to handle HTTP errors, expects (res, status, message) (default handles by setting response headers and ending the request).

Customizing Behavior

Changing Supported Methods and Content Types
const bodyParser = require('als-body-parser')({
  supportedMethods: ['POST'],
  supportedCt: ['application/json']
});

app.use(bodyParser);
Custom Error Handling and Logging
const bodyParser = require('als-body-parser')({
  httpErrorHandler: (res, status, message) => {
    res.status(status).send({ error: message });
  },
  logger: (error) => customLogger.error(error)
});

app.use(bodyParser);

Status Codes

The middleware can terminate the request with the following status codes under certain conditions:

  • 413 (Content Too Large): If the body exceeds the specified limit.
  • 400 (Bad Request): If the body cannot be parsed correctly (e.g., malformed JSON).
  • 408 (Request Timeout): If the full body is not received within the specified timeout.

req.body

Depending on the Content-Type, req.body may be:

  • An object, for JSON requests.
  • A string, for URL-encoded or plain text requests.
  • Undefined, if the content type is not supported or no body data is provided.

Keywords

FAQs

Package last updated on 18 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc