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

@medley/body-parser

v0.2.2

Published

Essential body parsers for Medley.

Downloads

11

Readme

@medley/body-parser

npm Version Build Status Coverage Status dependencies Status

Essential body parsers for Medley.

This module provides the following parsers:

Installation

npm install @medley/body-parser --save
# or
yarn add @medley/body-parser

API

const bodyParser = require('@medley/body-parser');

The bodyParser module exposes various factory functions that create a body-parsing onRequest/preHandler hook. All factory functions take an options object for configuration.

const bodyParser = require('@medley/body-parser');
const medley = require('@medley/medley');
const app = medley();

app.post('/user', {
  preHandler: bodyParser.json()
}, function handler(req, res) {
  req.body // Contains the request body
});

Note: Using body-parsers as a route-level preHandler rather than a global onRequest hook is better for performance and security since this avoids running the hook for requests that don’t need it (such as GET requests and requests that don’t match a route). This also gives you more control over where the body-parser runs, allowing you to ensure that it will only run after things like authentication and authorization hooks.


bodyParser.json([options])

Returns a hook that parses request bodies as JSON using JSON.parse().

Options

limit

Type: number Default: 102400 (100 KiB)

Specifies the maximum acceptable request body size.

bodyParser.json({limit: 100000})
type

Type: string | Array<string> | function Default: 'application/json'

Determines whether or not to parse the request body based on the request’s media type. If a MIME type string or array of strings, it uses compile-mime-match to match against the request’s Content-Type header. If a function, it is called as fn(req) and the request will be parsed if the function returns a truthy value.

bodyParser.json({type: '*/json'})
rejectUnsupportedTypes

Type: boolean Default: false

Throw a 415 Unsupported Media Type error if the request media type does not match the type option.


bodyParser.text([options])

Returns a hook that parses request bodies into a string.

Options

limit

Type: number Default: 102400 (100 KiB)

Specifies the maximum acceptable request body size.

bodyParser.text({limit: 100000})
type

Type: string | Array<string> | function Default: 'text/plain'

Determines whether or not to parse the request body based on the request’s media type. If a MIME type string or array of strings, it uses compile-mime-match to match against the request’s Content-Type header. If a function, it is called as fn(req) and the request will be parsed if the function returns a truthy value.

bodyParser.text({type: 'text/*'})
rejectUnsupportedTypes

Type: boolean Default: false

Throw a 415 Unsupported Media Type error if the request media type does not match the type option.


bodyParser.urlEncoded([options])

Returns a hook that parses URL-encoded request bodies into an object.

Options

limit

Type: number Default: 102400 (100 KiB)

Specifies the maximum acceptable request body size.

bodyParser.urlEncoded({limit: 100000})
type

Type: string | Array<string> | function Default: 'application/x-www-form-urlencoded'

Determines whether or not to parse the request body based on the request’s media type. If a MIME type string or array of strings, it uses compile-mime-match to match against the request’s Content-Type header. If a function, it is called as fn(req) and the request will be parsed if the function returns a truthy value.

bodyParser.urlEncoded({type: '*/x-www-form-urlencoded'})
rejectUnsupportedTypes

Type: boolean Default: false

Throw a 415 Unsupported Media Type error if the request media type does not match the type option.

parser

Type: function Default: querystring.parse

Specifies the function that will parse the request body from a string into an object. This can be used as a way to call querystring.parse() with options.

const querystring = require('querystring');

function customParser(body) {
  return querystring.parse(body, null, null, {maxKeys: 20});
}

bodyParser.urlEncoded({parser: customParser})

bodyParser.buffer([options])

Returns a hook that parses request bodies into a Buffer.

Options

limit

Type: number Default: 102400 (100 KiB)

Specifies the maximum acceptable request body size.

bodyParser.buffer({limit: 100000})
type

Type: string | Array<string> | function Default: 'application/octet-stream'

Determines whether or not to parse the request body based on the request’s media type. If a MIME type string or array of strings, it uses compile-mime-match to match against the request’s Content-Type header. If a function, it is called as fn(req) and the request will be parsed if the function returns a truthy value.

bodyParser.buffer({type: 'image/png'})

// Parse every request, regardless of its media type
bodyParser.buffer({type: () => true})
rejectUnsupportedTypes

Type: boolean Default: false

Throw a 415 Unsupported Media Type error if the request media type does not match the type option.


Reusable Hooks Pattern

To avoid having to create a new hook for every route that needs one, a body-parser can be attached to an app using the app.extend() method so it can easily be reused in multiple routes.

const medley = require('@medley/medley');
const bodyParser = require('@medley/body-parser');

const app = medley();

app.extend('jsonBodyParser', bodyParser.json({
  rejectUnsupportedTypes: true
}));

app.post('/user', {
  preHandler: app.jsonBodyParser
}, function handler(req, res) {
  // ...
});

app.post('/comment', {
  preHandler: app.jsonBodyParser
}, function handler(req, res) {
  // ...
});