A Stream of HTTP req/res pairs
var HttpStream = require("http-stream")
, chain = require("chain-stream")
, request = require("request")
var server = chain(HttpStream().listen(8080))
server
.filter(route("/hello.json"))
.forEach(function (dup) {
dup.setHeader("content-type", "application/json")
dup.end(JSON.stringify({ msg: "hello" }))
})
server
.filter(route("/plaintext"))
.forEach(function (dup) {
dup.setHeader("content-type", "text/plain")
dup.end("I like text/plain")
})
server
.filter(route("/"))
.filter(method("GET"))
.forEach(function (dup) {
dup.setHeader("content-type", "text/html")
chain(
request("http://me.iriscouch.com/db", {
json: true
}))
.map(function (chunk) {
return "<html><head>cool</head><body>" + chunk.index +
"</body></html>"
})
.pipe(dup)
})
function route(path) {
/* router implementation. A single matching on dup.url */
}
function method(methodName) {
return function (dup) {
return dup.method === methodName
}
}