Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first

  • Loading branch information...
commit e71c566a456af6535c2fd6c39ff467ab86759190 0 parents
@Raynos authored
3  .gitignore
@@ -0,0 +1,3 @@
+node_modules
+*.log
+*.err
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
19 LICENCE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Raynos.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
4 Makefile
@@ -0,0 +1,4 @@
+test:
+ ./node_modules/.bin/tap --stderr ./test
+
+.PHONY: test
22 README.md
@@ -0,0 +1,22 @@
+# routil-send [![build status][1]][2]
+
+send data through response
+
+## Example
+
+## Installation
+
+`npm install routil-send`
+
+## Tests
+
+`make test`
+
+## Contributors
+
+ - Raynos
+
+## MIT Licenced
+
+ [1]: https://secure.travis-ci.org/Raynos/routil-send.png
+ [2]: http://travis-ci.org/Raynos/routil-send
42 index.js
@@ -0,0 +1,42 @@
+var forEach = require("iterators").forEachSync
+
+module.exports = send
+
+send.sendJson = sendJson
+send.sendHtml = sendHtml
+
+function sendJson(res, object, statusCode) {
+ send(res, JSON.stringify(object), statusCode, {
+ "content-type": "application/json"
+ })
+}
+
+function sendHtml(res, data, statusCode) {
+ send(res, data, statusCode, {
+ "content-type": "text/html"
+ })
+}
+
+function send(res, data, statusCode, headers) {
+ if (typeof statusCode === "object") {
+ headers = statusCode
+ statusCode = null
+ }
+
+ if (!Buffer.isBuffer(data)) {
+ data = new Buffer(data)
+ }
+
+ headers = headers || {}
+ headers["content-length"] = data.length
+
+ res.statusCode = statusCode || res.statusCode || 200
+
+ forEach(headers, writeHeader, res)
+
+ res.end(data)
+}
+
+function writeHeader(value, headerName) {
+ this.setHeader(headerName, value)
+}
35 package.json
@@ -0,0 +1,35 @@
+{
+ "name": "routil-send",
+ "version": "0.0.1",
+ "description": "send data through response",
+ "keywords": [],
+ "author": "Raynos <raynos2@gmail.com>",
+ "repository": "git://github.com/Raynos/routil-send.git",
+ "main": "index",
+ "homepage": "https://github.com/Raynos/routil-send",
+ "contributors": [
+ {
+ "name": "Jake Verbaten"
+ }
+ ],
+ "bugs": {
+ "url": "https://github.com/Raynos/routil-send/issues",
+ "email": "raynos2@gmail.com"
+ },
+ "dependencies": {
+ "iterators": "~0.1.0"
+ },
+ "devDependencies": {
+ "tap": "0.2.5",
+ "test-server": "0.0.1"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "http://github.com/Raynos/routil-send/raw/master/LICENSE"
+ }
+ ],
+ "scripts": {
+ "test": "make test"
+ }
+}
68 test/integration.js
@@ -0,0 +1,68 @@
+var test = require("tap").test
+ , send = require("..")
+ , testServer = require("test-server")
+ , sendJson = send.sendJson
+ , sendHtml = send.sendHtml
+
+testServer(handleRequest, startTest)
+
+function handleRequest(req, res) {
+ if (req.url === "/send") {
+ send(res, "foo", 202, {
+ bar: "baz"
+ })
+ } else if (req.url === "/optional") {
+ send(res, "foo")
+ } else if (req.url === "/json") {
+ sendJson(res, {
+ foo: "bar"
+ }, 201)
+ } else if (req.url === "/html") {
+ sendHtml(res, "<div>foo</div>")
+ }
+}
+
+function startTest(request, done) {
+ test("send", function (t) {
+ request("/send", function (err, res, body) {
+ t.equal(body, "foo")
+ t.equal(res.statusCode, 202)
+ t.equal(res.headers.bar, "baz")
+
+ t.end()
+ })
+ })
+
+ test("optional", function (t) {
+ request("/optional", function (err, res, body) {
+ t.equal(body, "foo")
+ t.equal(res.statusCode, 200)
+
+ t.end()
+ })
+ })
+
+ test("json", function (t) {
+ request("/json", function (err, res, body) {
+ var data = JSON.parse(body)
+
+ t.equal(data.foo, "bar")
+ t.equal(res.statusCode, 201)
+ t.equal(res.headers['content-type'], "application/json")
+
+ t.end()
+ })
+ })
+
+ test("html", function (t) {
+ request("/html", function (err, res, body) {
+ t.equal(body, "<div>foo</div>")
+ t.equal(res.statusCode, 200)
+ t.equal(res.headers["content-type"], "text/html")
+
+ t.end()
+ })
+ })
+
+ .on("end", done)
+}
Please sign in to comment.
Something went wrong with that request. Please try again.