Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
send data through response
JavaScript OCaml

7.0.0

latest commit 2c1563883b
@Raynos authored

README.md

send-data build status

send data through response

Originally taken from npm-www

Example

var send = require("send-data")
var sendJson = require("send-data/json")
var sendHtml = require("send-data/html")
var sendError = require("send-data/error")
var http = require("http")

http.createServer(function handleRequest(req, res) {
    if (req.url === "/send") {
        send(req, res, {
            body: "foo",
            statusCode: 202,
            headers: {
                bar: "baz"
            }
        })
    } else if (req.url === "/optional") {
        send(req, res, "foo")
    } else if (req.url === "/json") {
        sendJson(req, res, {
            body: {
                foo: "bar"
            },
            statusCode: 201
        })
    } else if (req.url === "/json/optional") {
        sendJson(req, res, {
            foo: "bar"
        })
    } else if (req.url === "/html") {
        sendHtml(req, res, {
            body: "<div>foo</div>",
            statusCode: 200,
            headers: {}
        })
    } else if (req.url === "/html/optional") {
        sendHtml(req, res, "<div>foo</div>")
    } else if (req.url === "/oops") {
        sendError(req, res, { body: new Error("OOPS") })
    }
}).listen(8080)

Installation

npm install send-data

Tests

npm test

Contributors

  • Raynos

MIT Licenced

Something went wrong with that request. Please try again.