Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first

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

0 comments on commit e71c566

Please sign in to comment.
Something went wrong with that request. Please try again.