You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
blockvote/node_modules/body-parser/index.js

166 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*!
* body-parser
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* Module dependencies.
* @private
*/
var deprecate = require('depd')('body-parser')
/**
* Cache of loaded parsers.
* @private
*/
var parsers = Object.create(null)
/**
* @typedef Parsers
* @type {function}
* @property {function} json
* @property {function} raw
* @property {function} text
* @property {function} urlencoded
*/
/**
* Module exports.
* @type {Parsers}
*/
exports = module.exports = deprecate.function(bodyParser,
'bodyParser: use individual json/urlencoded middlewares')
/**
* JSON parser.
* @public
*/
Object.defineProperty(exports, 'json', {
configurable: true,
enumerable: true,
get: createParserGetter('json')
})
/**
* Raw parser.
* @public
*/
Object.defineProperty(exports, 'raw', {
configurable: true,
enumerable: true,
get: createParserGetter('raw')
})
/**
* Text parser.
* @public
*/
Object.defineProperty(exports, 'text', {
configurable: true,
enumerable: true,
get: createParserGetter('text')
})
/**
* URL-encoded parser.
* @public
*/
Object.defineProperty(exports, 'urlencoded', {
configurable: true,
enumerable: true,
get: createParserGetter('urlencoded')
})
/**
* Create a middleware to parse json and urlencoded bodies.
*
* @param {object} [options]
* @return {function}
* @deprecated
* @public
*/
// 定义一个 bodyParser 函数,接受一个可选的配置选项对象
function bodyParser(options) {
// 创建一个对象 opts继承自 options 对象。 如果没有传递 options使用 null。
// 在 opts 对象中,定义一个名为 'type' 的属性,默认值为 undefined允许修改其值。
var opts = Object.create(options || null, {
type: {
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)
// 返回一个新的中间件函数,接收 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
*/
function createParserGetter (name) {
return function get () {
return loadParser(name)
}
}
/**
* Load a parser module.
* @private
*/
function loadParser (parserName) {
var parser = parsers[parserName]
if (parser !== undefined) {
return parser
}
// this uses a switch for static require analysis
switch (parserName) {
case 'json':
parser = require('./lib/types/json')
break
case 'raw':
parser = require('./lib/types/raw')
break
case 'text':
parser = require('./lib/types/text')
break
case 'urlencoded':
parser = require('./lib/types/urlencoded')
break
}
// store to prevent invoking require()
return (parsers[parserName] = parser)
}