demo/body-parser/lib/read.js
function read (req, res, next, parse, debug, options) {
  var length
  var opts = options || {}
  var stream

  // flag as parsed
  req._body = true

  // read options
  var encoding = opts.encoding !== null
    ? opts.encoding || 'utf-8'
    : null
  var verify = opts.verify

  try {
    // get the content stream
    stream = contentstream(req, debug, opts.inflate)
    length = stream.length
    stream.length = undefined
  } catch (err) {
    return next(err)
  }

  // set raw-body options
  opts.length = length
  opts.encoding = verify
    ? null
    : encoding

  // assert charset is supported
  if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) {
    return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
      charset: encoding.toLowerCase()
    }))
  }

  // read body
  debug('read body')
  getBody(stream, opts, function (err, body) {
    if (err) {
      // default to 400
      setErrorStatus(err, 400)

      // echo back charset
      if (err.type === 'encoding.unsupported') {
        err = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
          charset: encoding.toLowerCase()
        })
      }

      // read off entire request
      stream.resume()
      onFinished(req, function onfinished () {
        next(err)
      })
      return
    }

    // verify
    if (verify) {
      try {
        debug('verify body')
        verify(req, res, body, encoding)
      } catch (err) {
        // default to 403
        setErrorStatus(err, 403)
        next(err)
        return
      }
    }

    // parse
    var str
    try {
      debug('parse body')
      str = typeof body !== 'string' && encoding !== null
        ? iconv.decode(body, encoding)
        : body
      req.body = parse(str)
    } catch (err) {
      err.body = str === undefined
        ? body
        : str

      // default to 400
      setErrorStatus(err, 400)

      next(err)
      return
    }

    next()
  })
}
function read (req, res, next, parse, debug, options) {
  var length
  var opts = options || {}
  var stream

  // flag as parsed
  req._body = true

  // read options
  var encoding = opts.encoding !== null
    ? opts.encoding || 'utf-8'
    : null
  var verify = opts.verify

  try {
    // get the content stream
    stream = contentstream(req, debug, opts.inflate)
    length = stream.length
    stream.length = undefined
  } catch (err) {
    return next(err)
  }

  // set raw-body options
  opts.length = length
  opts.encoding = verify
    ? null
    : encoding

  // assert charset is supported
  if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) {
    return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
      charset: encoding.toLowerCase()
    }))
  }

  // read body
  debug('read body')
  getBody(stream, opts, function (err, body) {
    if (err) {
      // default to 400
      setErrorStatus(err, 400)

      // echo back charset
      if (err.type === 'encoding.unsupported') {
        err = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
          charset: encoding.toLowerCase()
        })
      }

      // read off entire request
      stream.resume()
      onFinished(req, function onfinished () {
        next(err)
      })
      return
    }

    // verify
    if (verify) {
      try {
        debug('verify body')
        verify(req, res, body, encoding)
      } catch (err) {
        // default to 403
        setErrorStatus(err, 403)
        next(err)
        return
      }
    }

    // parse
    var str
    try {
      debug('parse body')
      str = typeof body !== 'string' && encoding !== null
        ? iconv.decode(body, encoding)
        : body
      req.body = parse(str)
    } catch (err) {
      err.body = str === undefined
        ? body
        : str

      // default to 400
      setErrorStatus(err, 400)

      next(err)
      return
    }

    next()
  })
}
function read ( req , res , next , parse , debug , options ) { var length var opts = options || { } var stream // flag as parsed req . _body = true // read options var encoding = opts . encoding !== null ? opts . encoding || 'utf-8' : null var verify = opts . verify try { // get the content stream stream = contentstream ( req , debug , opts . inflate ) length = stream . length stream . length = undefined } catch ( err ) { return next ( err ) } // set raw-body options opts . length = length opts . encoding = verify ? null : encoding // assert charset is supported if ( opts . encoding === null && encoding !== null && ! iconv . encodingExists ( encoding ) ) { return next ( createError ( 415 , 'unsupported charset "' + encoding . toUpperCase ( ) + '"' , { charset : encoding . toLowerCase ( ) } ) ) } // read body debug ( 'read body' ) getBody ( stream , opts , function ( err , body ) { if ( err ) { // default to 400 setErrorStatus ( err , 400 ) // echo back charset if ( err . type === 'encoding.unsupported' ) { err = createError ( 415 , 'unsupported charset "' + encoding . toUpperCase ( ) + '"' , { charset : encoding . toLowerCase ( ) } ) } // read off entire request stream . resume ( ) onFinished ( req , function onfinished ( ) { next ( err ) } ) return } // verify if ( verify ) { try { debug ( 'verify body' ) verify ( req , res , body , encoding ) } catch ( err ) { // default to 403 setErrorStatus ( err , 403 ) next ( err ) return } } // parse var str try { debug ( 'parse body' ) str = typeof body !== 'string' && encoding !== null ? iconv . decode ( body , encoding ) : body req . body = parse ( str ) } catch ( err ) { err . body = str === undefined ? body : str // default to 400 setErrorStatus ( err , 400 ) next ( err ) return } next ( ) } ) }
Read a request into a buffer and parse.

@param {object} req
@param {object} res
@param {function} next
@param {function} parse
@param {function} debug
@param {object} [options]
@api private
Read a request into a buffer and parse .