|
|
|
|
@ -90,28 +90,37 @@ Object.defineProperty(exports, 'urlencoded', {
|
|
|
|
|
* @public
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function bodyParser (options) {
|
|
|
|
|
// use default type for parsers
|
|
|
|
|
// 定义一个 bodyParser 函数,接受一个可选的配置选项对象
|
|
|
|
|
function bodyParser(options) {
|
|
|
|
|
// 创建一个对象 opts,继承自 options 对象。 如果没有传递 options,使用 null。
|
|
|
|
|
// 在 opts 对象中,定义一个名为 'type' 的属性,默认值为 undefined,允许修改其值。
|
|
|
|
|
var opts = Object.create(options || null, {
|
|
|
|
|
type: {
|
|
|
|
|
configurable: true,
|
|
|
|
|
enumerable: true,
|
|
|
|
|
value: undefined,
|
|
|
|
|
writable: true
|
|
|
|
|
configurable: true, // 该属性可以被重新定义
|
|
|
|
|
enumerable: true, // 该属性会出现在枚举操作中(如 for...in)
|
|
|
|
|
value: undefined, // 默认值为 undefined
|
|
|
|
|
writable: true // 该属性的值可以被修改
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 调用 exports 中的 urlencoded 和 json 函数,分别返回解析请求体为 URL 编码格式和 JSON 格式的中间件
|
|
|
|
|
var _urlencoded = exports.urlencoded(opts)
|
|
|
|
|
var _json = exports.json(opts)
|
|
|
|
|
|
|
|
|
|
return function bodyParser (req, res, next) {
|
|
|
|
|
_json(req, res, function (err) {
|
|
|
|
|
// 返回一个新的中间件函数,接收 req, res 和 next 作为参数
|
|
|
|
|
return function bodyParser(req, res, next) {
|
|
|
|
|
// 首先调用 _json 中间件解析请求体为 JSON 格式
|
|
|
|
|
_json(req, res, function(err) {
|
|
|
|
|
// 如果解析 JSON 时出错,调用 next(err) 抛出错误
|
|
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
|
|
// 如果 JSON 解析没有问题,则继续调用 _urlencoded 中间件解析 URL 编码格式
|
|
|
|
|
_urlencoded(req, res, next)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a getter for loading a parser.
|
|
|
|
|
* @private
|
|
|
|
|
|