demo/body-parser/lib/types/urlencoded.js
function urlencoded (options) {
  var opts = options || {}

  // notice because option default will flip in next major
  if (opts.extended === undefined) {
    deprecate('undefined extended: provide extended option')
  }

  var extended = opts.extended !== false
  var inflate = opts.inflate !== false
  var limit = typeof opts.limit !== 'number'
    ? bytes.parse(opts.limit || '100kb')
    : opts.limit
  var type = opts.type || 'application/x-www-form-urlencoded'
  var verify = opts.verify || false

  if (verify !== false && typeof verify !== 'function') {
    throw new TypeError('option verify must be function')
  }

  // create the appropriate query parser
  var queryparse = extended
    ? extendedparser(opts)
    : simpleparser(opts)

  // create the appropriate type checking function
  var shouldParse = typeof type !== 'function'
    ? typeChecker(type)
    : type

  function parse (body) {
    return body.length
      ? queryparse(body)
      : {}
  }

  return function urlencodedParser (req, res, next) {
    if (req._body) {
      debug('body already parsed')
      next()
      return
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      debug('skip empty body')
      next()
      return
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      debug('skip parsing')
      next()
      return
    }

    // assert charset
    var charset = getCharset(req) || 'utf-8'
    if (charset !== 'utf-8') {
      debug('invalid charset')
      next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
        charset: charset
      }))
      return
    }

    // read
    read(req, res, next, parse, debug, {
      debug: debug,
      encoding: charset,
      inflate: inflate,
      limit: limit,
      verify: verify
    })
  }
}
function urlencoded (options) {
  var opts = options || {}

  // notice because option default will flip in next major
  if (opts.extended === undefined) {
    deprecate('undefined extended: provide extended option')
  }

  var extended = opts.extended !== false
  var inflate = opts.inflate !== false
  var limit = typeof opts.limit !== 'number'
    ? bytes.parse(opts.limit || '100kb')
    : opts.limit
  var type = opts.type || 'application/x-www-form-urlencoded'
  var verify = opts.verify || false

  if (verify !== false && typeof verify !== 'function') {
    throw new TypeError('option verify must be function')
  }

  // create the appropriate query parser
  var queryparse = extended
    ? extendedparser(opts)
    : simpleparser(opts)

  // create the appropriate type checking function
  var shouldParse = typeof type !== 'function'
    ? typeChecker(type)
    : type

  function parse (body) {
    return body.length
      ? queryparse(body)
      : {}
  }

  return function urlencodedParser (req, res, next) {
    if (req._body) {
      debug('body already parsed')
      next()
      return
    }

    req.body = req.body || {}

    // skip requests without bodies
    if (!typeis.hasBody(req)) {
      debug('skip empty body')
      next()
      return
    }

    debug('content-type %j', req.headers['content-type'])

    // determine if request should be parsed
    if (!shouldParse(req)) {
      debug('skip parsing')
      next()
      return
    }

    // assert charset
    var charset = getCharset(req) || 'utf-8'
    if (charset !== 'utf-8') {
      debug('invalid charset')
      next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
        charset: charset
      }))
      return
    }

    // read
    read(req, res, next, parse, debug, {
      debug: debug,
      encoding: charset,
      inflate: inflate,
      limit: limit,
      verify: verify
    })
  }
}
function urlencoded ( options ) { var opts = options || { } // notice because option default will flip in next major if ( opts . extended === undefined ) { deprecate ( 'undefined extended: provide extended option' ) } var extended = opts . extended !== false var inflate = opts . inflate !== false var limit = typeof opts . limit !== 'number' ? bytes . parse ( opts . limit || '100kb' ) : opts . limit var type = opts . type || 'application/x-www-form-urlencoded' var verify = opts . verify || false if ( verify !== false && typeof verify !== 'function' ) { throw new TypeError ( 'option verify must be function' ) } // create the appropriate query parser var queryparse = extended ? extendedparser ( opts ) : simpleparser ( opts ) // create the appropriate type checking function var shouldParse = typeof type !== 'function' ? typeChecker ( type ) : type function parse ( body ) { return body . length ? queryparse ( body ) : { } } return function urlencodedParser ( req , res , next ) { if ( req . _body ) { debug ( 'body already parsed' ) next ( ) return } req . body = req . body || { } // skip requests without bodies if ( ! typeis . hasBody ( req ) ) { debug ( 'skip empty body' ) next ( ) return } debug ( 'content-type %j' , req . headers [ 'content-type' ] ) // determine if request should be parsed if ( ! shouldParse ( req ) ) { debug ( 'skip parsing' ) next ( ) return } // assert charset var charset = getCharset ( req ) || 'utf-8' if ( charset !== 'utf-8' ) { debug ( 'invalid charset' ) next ( createError ( 415 , 'unsupported charset "' + charset . toUpperCase ( ) + '"' , { charset : charset } ) ) return } // read read ( req , res , next , parse , debug , { debug : debug , encoding : charset , inflate : inflate , limit : limit , verify : verify } ) } }
Create a middleware to parse urlencoded bodies.

@param {object} [options]
@return {function}
@public
Create a middleware to parse urlencoded bodies .