http-body-json-parse

5.0.1 • Public • Published

http-json-body-parse

Parse the JSON body of an http request.

Usage

parse(
  request, // like the one from Node.js http

  /*
  The maximum buffer size (bytes) before throwing a `parse.BufferError`.
  This is optional but probably a good idea and defaults to `Infinity`.
  */
  maxBufferBytes,

  console.log // Optional logging function defaults to a no-op
)
const parse = require('http-body-json-parse')

http.createServer((request, response) => {
  parse(request, 50).then(console.log, e => {
    if (e instanceof parse.ContentTypeError) console.error(e.message)
    else if (e instanceof parse.ParsingError) console.error(e.message)
    else if (e instanceof parse.BufferError) console.error(e.message)
    else throw e
  })

  // introspection, great for post mortem
  request[parse.parsing] // equal to parse(request)
  request[parse.parsed] // equal to resolved parse(request)
})

Package Sidebar

Install

npm i http-body-json-parse

Weekly Downloads

1

Version

5.0.1

License

MIT

Unpacked Size

6.79 kB

Total Files

4

Last publish

Collaborators

  • thomas-jensen