body
Body parsing
Originally taken from npm-www
Example
var textBody = var jsonBody = var formBody = var anyBody = var http = var sendJson = http
body
simply parses the request body and returns it in the callback. jsonBody
and formBody
call JSON.parse and querystring.parse respectively on the body.
anyBody will detect the content-type of the request and use the appropiate body method.
Example generators
You can use body
with generators as the body functions will
return a continuable if you don't pass a callback.
var http = var Router = var jsonBody = var formBody = // async turns a generator into an async function taking a cbvar async = // the router works with normal async functions.// router automatically handles errors as 500 responsesvar app = app app // app returned from the router is just a function(req, res) {}// that dispatches the req/res to the correct route based on// the routers routing table & req.urlhttp
Documentation
textBody(req, res?, opts?, cb<Error, String>)
textBody := ( req: HttpRequest, res?: HttpResponse, opts?: { limit?: Number, cache?: Boolean, encoding?: String }, cb: Callback<err: Error, bodyPayload: String>) => void
textBody
allows you to get the body from any readable stream.
It will read the entire content of the stream into memory and
give it back to you in the callback.
limit
: You can setopts.limit
to a custom number to change the limit at whichtextBody
gives up. By default it will only read a 1MB body, if a stream contains more then 1MB it returns an error. This prevents someone attacking your HTTP server with an infinite body causing an out of memory attack.encoding
: You can setencoding
. All encodings that are valid on aBuffer
are valid options. It defaults to'utf8'
var textBody = var http = http
formBody(req, res?, opts?, cb<Error, Any>)
formBody := ( req: HttpRequest, res?: HttpResponse, opts?: { limit?: Number, encoding?: String, querystring: { parse: (String, Callback<Error, Any>) => void } }, cb: Callback<err: Error, bodyPayload: Any>) => void
formBody
allows you to get the body of a readable stream. It
does the same as textBody
but assumes the content is querystring
encoded and parses just like it was a <form> submit.
limit
: same astextBody
encoding
: same astextBody
querystring
: You can pass a custom querystring parser if you want. It should have aparse
method that takes a string and a callback. It should return the value in the callback or a parsing error
var formBody = var http = http
jsonBody(req, res?, opts?, cb<Error, Any>)
jsonBody := ( req: HttpRequest, res?: HttpResponse, opts?: { limit?: Number, encoding?: String, reviver?: (Any) => Any JSON?: { parse: (String, reviver?: Function, Callback<Error, Any>) => void } }, cb: Callback<err: Error, bodyPayload: Any>) => void
jsonBody
allows you to get the body of a readable stream. It
does the same as textbody
but assumes the content it a JSON
value and parses it using JSON.parse
. If JSON.parse
throws
an exception then it calls the callback with the exception.
limit
: same astextBody
encoding
: same astextBody
reviver
: A reviver function that will be passed toJSON.parse
as the second argumentJSON
: You can pass a custom JSON parser if you want. It should have aparse
method that takes a string, an optional reviver and a callback. It should return the value in the callback or a parsing error.
var jsonBody = var http = http
anyBody(req, res?, opts?, cb<Error, Any>)
anyBody := ( req: HttpRequest, res?: HttpResponse, opts?: { limit?: Number, encoding?: String, reviver?: (Any) => Any JSON?: { parse: (String, reviver?: Function, Callback<Error, Any>) => void }, querystring: { parse: (String, Callback<Error, Any>) => void } }, cb: Callback<err: Error, bodyPayload: Any>) => void
anyBody
allows you to get the body of a HTTPRequest. It
does the same as textBody
except it parses the content-type
header and uses either the jsonBody or the formBody function.
This allows you to write POST route handlers that work with both ajax and html form submits.
limit
: same astextBody
encoding
: same astextBody
reviver
: same asjsonBody
JSON
: same asjsonBody
querystring
: same asformBody
var anyBody = var http = http
Installation
npm install body
Tests
npm test
Contributors
- Raynos