Body parsing
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.
var Body = require("routil-body")
, bodyParser = Body()
, body = bodyParser.body
, formBody = bodyParser.formBody
, jsonBody = bodyParser.jsonBody
, anyBody = bodyParser.anyBody
, http = require("http")
http.createServer(function (req, res) {
if (req.url === '/json') {
jsonBody(req, res, function (body) {
res.end(JSON.stringify(body))
})
} else if (req.url === '/form') {
formbody(req, res, function (body) {
res.end(JSON.stringify(body))
})
} else if (req.url === '/any') {
anyBody(req, res, function (body) {
res.end(JSON.stringify(body))
})
} else {
body(req, function (body) {
res.end(body.toString())
})
}
}).listen(8080)
var jsonBody = require("routil-body")({
errorPage: function (req, res, errorData) {
// errorData is either a single value or an array of values
// the values are either a number for the HTTP response code
// or an object error object
}
}).jsonBody
, http = require('http')
http.createServer(function (req, res) {
jsonBody(req, function (body) { res.end(body) })
}).listen(8080)
npm install routil-body
make test
- Raynos