Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@medley/body-parser
Advanced tools
Essential body parsers for Medley.
This module provides the following parsers:
npm install @medley/body-parser --save
# or
yarn add @medley/body-parser
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()
.
Type: number
Default: 102400
(100 KiB)
Specifies the maximum acceptable request body size.
bodyParser.json({limit: 100000})
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'})
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
.
Type: number
Default: 102400
(100 KiB)
Specifies the maximum acceptable request body size.
bodyParser.text({limit: 100000})
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/*'})
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
.
Type: number
Default: 102400
(100 KiB)
Specifies the maximum acceptable request body size.
bodyParser.urlEncoded({limit: 100000})
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'})
Type: boolean
Default: false
Throw a 415 Unsupported Media Type
error if the request media type does not
match the type
option.
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
.
Type: number
Default: 102400
(100 KiB)
Specifies the maximum acceptable request body size.
bodyParser.buffer({limit: 100000})
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})
Type: boolean
Default: false
Throw a 415 Unsupported Media Type
error if the request media type does not
match the type
option.
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) {
// ...
});
FAQs
Essential body parsers for Medley.
The npm package @medley/body-parser receives a total of 1 weekly downloads. As such, @medley/body-parser popularity was classified as not popular.
We found that @medley/body-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.