Skip to content

srcagency/http-body-json-parse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
})

About

Parse the JSON body of an http request

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published