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