0.0.2 • Published 12 years ago

routil-body v0.0.2

Weekly downloads
2
License
-
Repository
github
Last release
12 years ago

routil-body build status

Body parsing

Example

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, function (body) {
            res.end(JSON.stringify(body))
        })
    } else if (req.url === '/form') {
        formbody(req, function (body) {
            res.end(JSON.stringify(body))
        })
    } else if (req.url === '/any') {
        anyBody(req, function (body) {
            res.end(JSON.stringify(body))
        })
    } else {
        body(req, function (body) {
            res.end(body.toString())
        })
    }
}).listen(8080)

Example with custom error handling

var body = 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
        }
    }).body
    , http = require('http')

http.createServer(function (req, res) {
    body(req, function (body) { res.end(body) })
}).listen(8080)

Installation

npm install routil-body

Tests

make test

Contributors

  • Raynos

MIT Licenced