BodyParser : Web body parser

更新时间:
2024-05-15
下载文档

BodyParser : Web body parser

Parse incoming request bodies in a middleware before your handlers, available under the req.body property.

This module provides the following parsers:

  • JSON body parser
  • Raw body parser
  • Text body parser
  • URL-encoded form 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.

User can use the following code to import the bodyParser module.

var bodyParser = require('middleware').bodyParser;

Support

The following shows bodyParser module APIs available for each permissions.

 User ModePrivilege Mode
bodyParser.json
bodyParser.raw
bodyParser.text
bodyParser.urlencoded

BodyParser Object

bodyParser.json([options])

  • options {Object} contain any of the following keys:
    • limit {Number | String} 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. default: '100kb'.
    • strict {Boolean} When set to true, will only accept arrays and objects; when false will accept anything JSON.parse accepts. Defaults to true.
    • reviver The reviver option is passed directly to JSON.parse as the second argument.
    • type {String | Array | Function} 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. default: 'application/json'.
  • Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option.

A new body object containing the parsed data is populated on the requestobject after the middleware (i.e. req.body).

bodyParser.raw([options])

  • options {Object} The raw function takes an optional options object that may contain any of the following keys:
    • limit {Number | String} 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. default: '100kb'.
    • type {String | Array | Function} 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. default: 'application/octet-stream'.
  • Returns middleware that parses all bodies as a Buffer and only looks at requests where the Content Type header matches the type option.

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.

bodyParser.text([options])

  • options {Object} The text function takes an optional options object that may contain any of the following keys:
    • limit {Number | String} 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. default: '100kb'.
    • type {String | Array | Function} 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 txt), a mime type (like text/plain), or a mime type with a wildcard (like */* or text/*). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. default: 'text/plain'.
  • Returns middleware that parses all bodies as a string and only looks at requests where the Content-Type header matches the type option.

A new body string containing the parsed data is populated on the requestobject after the middleware (i.e. req.body). This will be a string of the body.

bodyParser.urlencoded([options])

  • options {Object} The text function takes an optional options object that may contain any of the following keys:
    • limit {Number | String} 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. default: '100kb'.
    • type {String|Array|Function} 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 urlencoded), a mime type (like application/x-www-form-urlencoded), or a mime type with a wildcard (like */x-www-form-urlencoded). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. default: 'application/x-www-form-urlencoded'.
    • parameterLimit {Integer} The parameterLimit option controls the maximum number of parameters that are allowed in the URL-encoded data. If a request contains more parameters than this value, a 413 will be returned to the client. default: 1000.
  • Returns middleware that only parses urlencoded bodies and only looks at requests where the Content-Type header matches the type option.

A new body string containing the parsed data is populated on the requestobject after the middleware (i.e. req.body). This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).

Examples

  • This example demonstrates adding a generic JSON and URL-encoded parser as a btop-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup.
var socket = require('socket');
var WebApp = require('webapp');
var iosched = require('iosched');
var bodyParser = require('middleware').bodyParser;

// Create app.
var app = WebApp.create('app', 0, socket.sockaddr(socket.INADDR_ANY, 8000));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded());

// parse application/json
app.use(bodyParser.json());

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain');
  res.write('you posted:\n');
  res.end(JSON.stringify(req.body, null, 2));
});

// Event loop.
while (true) {
  iosched.poll();
}
  • All the parsers accept a type option which allows you to change the Content-Type that the middleware will parse.
var socket = require('socket');
var WebApp = require('webapp');
var bodyParser = require('middleware').bodyParser;

// Create app.
var app = WebApp.create('app', 0, socket.sockaddr(socket.INADDR_ANY, 8000));

// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }));

// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }));

// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }));
文档内容是否对您有所帮助?
有帮助
没帮助