run-static-analysis.sh
body-parser
Node.js body parsing middleware.
Parse incoming request bodies in a middleware before your handlers, available
under the req.body
property.
Learn about the anatomy of an HTTP transaction in Node.js.
This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:
This module provides the following parsers:
Other body parsers you might be interested in:
Installation
$ npm install body-parser
API
var bodyParser = require('body-parser')
The bodyParser
object exposes various factories to create middlewares. All
middlewares will populate the req.body
property with the parsed body when
the Content-Type
request header matches the type
option, or an empty
object ({}
) if there was no body to parse, the Content-Type
was not matched,
or an error occurred.
The various errors returned by this module are described in the errors section.
bodyParser.json(options)
Returns middleware that only parses json
and only looks at requests where
the Content-Type
header matches the type
option. This parser accepts any
Unicode encoding of the body and supports automatic inflation of gzip
and
deflate
encodings.
A new body
object containing the parsed data is populated on the request
object after the middleware (i.e. req.body
).
Options
The json
function takes an option options
object that may contain any of
the following keys:
inflate
When set to true
, then deflated (compressed) bodies will be inflated; when
false
, deflated bodies are rejected. Defaults to true
.
limit
Controls the maximum request body size. If this is a number, then the value
specifies the number of bytes; if it is a string, the value is passed to the
bytes library for parsing. Defaults
to '100kb'
.
reviver
The reviver
option is passed directly to JSON.parse
as the second
argument. You can find more information on this argument
in the MDN documentation about JSON.parse.
strict
When set to true
, will only accept arrays and objects; when false
will
accept anything JSON.parse
accepts. Defaults to true
.