Browse files

cleanup and fixes

  • Loading branch information...
1 parent c43708a commit 2ebfa43db42332bd61184465ed80ab1f83e9d434 @Raynos committed Sep 23, 2012
Showing with 96 additions and 164 deletions.
  1. +0 −3 .npmignore
  2. +0 −10 Makefile
  3. +35 −49 README.md
  4. +38 −62 index.js
  5. +9 −11 package.json
  6. +14 −15 test/integration.js
  7. +0 −14 testem.json
View
3 .npmignore
@@ -1,3 +0,0 @@
-node_modules
-*.log
-*.err
View
10 Makefile
@@ -1,10 +0,0 @@
-node-test:
- ./node_modules/.bin/tap --stderr ./test
-
-test:
- ./node_modules/.bin/testem \
- --file testem.json \
- --debuglog testem.log \
- --debug 2> testem.err
-
-.PHONY: test
View
84 README.md
@@ -1,69 +1,55 @@
-# routil-body [![build status][1]][2]
+# body [![build status][1]][2]
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.
+```
+var body = require("body")
+ , jsonBody = body.json
+ , formBody = body.form
+ , anyBody = body.any
+ , http = require("http")
+ , sendJson = require("send-data").json
+
+http.createServer(function handleRequest(req, res) {
+ if (req.url === "/body") {
+ body(req, res, function (err, body) {
+ sendJson(req, res, body)
+ })
+ } else if (req.url === "/form") {
+ formBody(req, res, function (err, body) {
+ sendJson(req, res, body)
+ })
+ } else if (req.url === "/json") {
+ jsonBody(req, res, function (err, body) {
+ sendJson(req, res, body)
+ })
+ } else if (req.url === "/any") {
+ anyBody(req, res, function (err, body) {
+ sendJson(req, res, body)
+ })
+ }
+})
+```
+
+`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)
-
-## Example with custom error handling
-
- 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)
-
## Installation
-`npm install routil-body`
+`npm install body`
## Tests
-`make test`
+`npm test`
## Contributors
- Raynos
## MIT Licenced
- [1]: https://secure.travis-ci.org/Raynos/routil-body.png
- [2]: http://travis-ci.org/Raynos/routil-body
+ [1]: https://secure.travis-ci.org/Raynos/body.png
+ [2]: http://travis-ci.org/Raynos/body
View
100 index.js
@@ -1,66 +1,16 @@
var StringDecoder = require("string_decoder").StringDecoder
, querystring = require("querystring")
- , routil = require("routil")
- , routilErrorPage = routil.errorPage
- , contentTypes = routil.contentTypes
- , isJSON = /\/(x-)?json$/
- , isForm = /application\/x\-www\-form\-urlencoded/
+ , contentTypes = require("content-types")
-module.exports = Body
+body.form = formBody
+body.json = jsonBody
+body.any = anyBody
-function Body(options) {
- var errorPage = routilErrorPage || options.errorPage
+module.exports = body
- return {
- body: body,
- formBody: formBody,
- jsonBody: jsonBody,
- anyBody: anyBody
- }
-
- function formBody(req, res, callback) {
- var contentType = req.headers['content-type'] || ""
- if (!contentType.match(isForm)) {
- // XXX Add support for formidable uploading, as well
- return errorPage(req, res, 415)
- }
- body(req, parseBody)
-
- function parseBody(body) {
- callback(querystring.parse(body))
- }
- }
-
- function jsonBody(req, res, callback) {
- var contentType = req.headers['content-type'] || ""
- if (!contentType.match(isJSON)) {
- return errorPage(req, res, 415)
- }
- body(req, extractJSON)
-
- function extractJSON(body) {
- var json
- try {
- json = JSON.parse(body)
- } catch (error) {
- return errorPage(req, res, [400, error])
- }
- callback(json)
- }
- }
-
- function anyBody(req, res, callback) {
- contentTypes(req, {
- "application/json": jsonBody,
- "application/x-www-form-urlencoded": formBody,
- "default": defaultAnyBodyHandler
- })(req, res, callback)
- }
-}
-
-function body(req, callback) {
- if (req.__routil_body__) {
- callback(req.__routil_body__)
+function body(req, res, callback) {
+ if (req.__body__) {
+ callback(req.__body__)
}
var requestBody = "",
@@ -75,11 +25,37 @@ function body(req, callback) {
}
function returnBody() {
- req.__routil_body__ = requestBody
- callback(requestBody)
+ req.__body__ = requestBody
+ callback(null, requestBody)
+ }
+}
+
+function anyBody(req, res, callback) {
+ contentTypes(req, res, {
+ "application/json": jsonBody,
+ "application/x-www-form-urlencoded": formBody,
+ "default": body
+ })(req, res, callback)
+}
+
+function formBody(req, res, callback) {
+ body(req, res, parseBody)
+
+ function parseBody(err, body) {
+ callback(null, querystring.parse(body))
}
}
-function defaultAnyBodyHandler(req, res, callback) {
- body(req, callback)
+function jsonBody(req, res, callback) {
+ body(req, res, extractJSON)
+
+ function extractJSON(err, body) {
+ var json
+ try {
+ json = JSON.parse(body)
+ } catch (error) {
+ return callback(error)
+ }
+ callback(null, json)
+ }
}
View
20 package.json
@@ -1,39 +1,37 @@
{
- "name": "routil-body",
+ "name": "body",
"version": "0.0.2",
"description": "Body parsing",
"keywords": [],
"author": "Raynos <raynos2@gmail.com>",
- "repository": "git://github.com/Raynos/routil-body.git",
+ "repository": "git://github.com/Raynos/body.git",
"main": "index",
- "homepage": "https://github.com/Raynos/routil-body",
+ "homepage": "https://github.com/Raynos/body",
"contributors": [
{
"name": "Jake Verbaten"
}
],
"bugs": {
- "url": "https://github.com/Raynos/routil-body/issues",
+ "url": "https://github.com/Raynos/body/issues",
"email": "raynos2@gmail.com"
},
"dependencies": {
- "routil": "0.0.14"
+ "content-types": "~0.1.0"
},
"devDependencies": {
- "testling": "git://github.com/Raynos/testling#master",
- "testem": "git://github.com/Raynos/testem#master",
- "sinon": "1.4.2",
"tap": "0.2.5",
"after": "~0.6.0",
- "test-server": "0.0.1"
+ "test-server": "0.0.1",
+ "send-data": "~0.1.0"
},
"licenses": [
{
"type": "MIT",
- "url": "http://github.com/Raynos/routil-body/raw/master/LICENSE"
+ "url": "http://github.com/Raynos/body/raw/master/LICENSE"
}
],
"scripts": {
- "test": "make node-test"
+ "test": "tap --stderr --tap ./test"
}
}
View
29 test/integration.js
@@ -1,31 +1,30 @@
var testServer = require("test-server")
- , test = require("testling")
- , routilBody = require("..")()
- , body = routilBody.body
- , formBody = routilBody.formBody
- , jsonBody = routilBody.jsonBody
- , anyBody = routilBody.anyBody
- , sendJson = require("routil").sendJson
+ , test = require("tap").test
+ , body = require("../index")
+ , formBody = body.form
+ , jsonBody = body.json
+ , anyBody = body.any
+ , sendJson = require("send-data").json
, after = require("after")
testServer(handleRequest, runTests)
function handleRequest(req, res) {
if (req.url === "/body") {
- body(req, function (body) {
- sendJson(res, body)
+ body(req, res, function (err, body) {
+ sendJson(req, res, body)
})
} else if (req.url === "/form") {
- formBody(req, res, function (body) {
- sendJson(res, body)
+ formBody(req, res, function (err, body) {
+ sendJson(req, res, body)
})
} else if (req.url === "/json") {
- jsonBody(req, res, function (body) {
- sendJson(res, body)
+ jsonBody(req, res, function (err, body) {
+ sendJson(req, res, body)
})
} else if (req.url === "/any") {
- anyBody(req, res, function (body) {
- sendJson(res, body)
+ anyBody(req, res, function (err, body) {
+ sendJson(req, res, body)
})
}
}
View
14 testem.json
@@ -1,14 +0,0 @@
-{
- "framework": "custom"
- , "browserify": true
- , "src_files": [
- "test/**/*.js"
- , "node_modules/testem/adapters/testling.js"
- ]
- , "watch_files": [
- "test/**/*.js"
- , "./index.js"
- ]
- , "node": "node_modules/.bin/tap"
- , "node-args": ["--tap", "test"]
-}

0 comments on commit 2ebfa43

Please sign in to comment.