You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Return the first accepted charset. If nothing in charsets is accepted,
then false is returned.
.charsets()
Return the charsets that the request accepts, in the order of the client's
preference (most preferred first).
.encoding(encodings)
Return the first accepted encoding. If nothing in encodings is accepted,
then false is returned.
.encodings()
Return the encodings that the request accepts, in the order of the client's
preference (most preferred first).
.language(languages)
Return the first accepted language. If nothing in languages is accepted,
then false is returned.
.languages()
Return the languages that the request accepts, in the order of the client's
preference (most preferred first).
.type(types)
Return the first accepted type (and it is returned as the same text as what
appears in the types array). If nothing in types is accepted, then false
is returned.
The types array can contain full MIME types or file extensions. Any value
that is not a full MIME types is passed to require('mime-types').lookup.
.types()
Return the types that the request accepts, in the order of the client's
preference (most preferred first).
Examples
Simple type negotiation
This simple example shows how to use accepts to return a different typed
respond body based on what the client wants to accept. The server lists it's
preferences in order and will get back the best match between the client and
server.
varaccepts=require('accepts')varhttp=require('http')functionapp(req,res){varaccept=accepts(req)// the order of this list is significant; should be server preferred orderswitch(accept.type(['json','html'])){case'json':
res.setHeader('Content-Type','application/json')res.write('{"hello":"world!"}')breakcase'html':
res.setHeader('Content-Type','text/html')res.write('<b>hello, world!</b>')breakdefault:
// the fallback is text/plain, so no need to specify it aboveres.setHeader('Content-Type','text/plain')res.write('hello, world!')break}res.end()}http.createServer(app).listen(3000)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parse incoming request bodies in a middleware before your handlers, available
under the req.body property.
Note As req.body's shape is based on user-controlled input, all
properties and values in this object are untrusted and should be validated
before trusting. For example, req.body.foo.toString() may fail in multiple
ways, for example the foo property may not be there or may not be a string,
and toString may not be a function and instead a string or other user input.
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:
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 optional 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.
type
The type option is used to determine what media type the middleware will
parse. This option can be a string, array of strings, or a function. If not a
function, type option is passed directly to the
type-is library and this can
be an extension name (like json), a mime type (like application/json), or
a mime type with a wildcard (like */* or */json). If a function, the type
option is called as fn(req) and the request is parsed if it returns a truthy
value. Defaults to application/json.
verify
The verify option, if supplied, is called as verify(req, res, buf, encoding),
where buf is a Buffer of the raw request body and encoding is the
encoding of the request. The parsing can be aborted by throwing an error.
bodyParser.raw([options])
Returns middleware that parses all bodies as a Buffer and only looks at
requests where the Content-Type header matches the type option. This
parser 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). This will be a Buffer object
of the body.
Options
The raw function takes an optional 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'.
type
The type option is used to determine what media type the middleware will
parse. This option can be a string, array of strings, or a function.
If not a function, type option is passed directly to the
type-is library and this
can be an extension name (like bin), a mime type (like
application/octet-stream), or a mime type with a wildcard (like */* or
application/*). If a function, the type option is called as fn(req)
and the request is parsed if it returns a truthy value. Defaults to
application/octet-stream.
verify
The verify option, if supplied, is called as verify(req, res, buf, encoding),
where buf is a Buffer of the raw request body and encoding is the
encoding of the request. The parsing can be aborted by throwing an error.
bodyParser.text([options])
Returns middleware that parses all bodies as a string and only looks at
requests where the Content-Type header matches the type option. This
parser supports automatic inflation of gzip and deflate encodings.
A new body string containing the parsed data is populated on the request
object after the middleware (i.e. req.body). This will be a string of the
body.
Options
The text function takes an optional options object that may contain any of
the following keys:
defaultCharset
Specify the default character set for the text content if the charset is not
specified in the Content-Type header of the request. Defaults to utf-8.
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'.