npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

als-body-parser

v1.0.0

Published

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

Downloads

5

Readme

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.