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

@medley/body-parser

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@medley/body-parser

Essential body parsers for Medley.

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

@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) {
  // ...
});

Keywords

FAQs

Package last updated on 02 Nov 2019

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