Imagine a "middleware framework" that is based purely of streams
The main idea is route
route(uri, FunctionThatReturnsStream, FunctionThatReturnsADifferentStream, ...)
Which ends up adding a route to the router
router.addRoute(uri, function (req, res) {
req
.pipe(FunFunctionThatReturnsStream(req, res))
.pipe(FunctionThatReturnsADifferentStream(req, res))
.pipe(res)
})
var streams = require("middleware-streams")
// partial function application
, partial = streams.partial
// returns a function which calls the first argument with no arguments
, call = streams.call
// read-stream
, from = streams.from
// through-stream
, through = streams.through
, filed = require("filed")
, path = require("path")
, es = require("event-stream")
var handler = streams({ /* config???? */ })
// route(static, function () { return filed(path.join(__dirname, "static") }
.route("/static/*", function () {
return filed(path.join(__dirname, "static"))
})
.route("/proxypass", function (req) {
return request("http://otherserver.com" + req.url)
})
.route("/hello.json"
, function () {
return from([{ msg: "hello" }])
}
, function () {
return es.mapSync(JSON.stringify)
}
, contentType("application/json")
)
.route("/plaintext"
, function () {
return from(["I like text/plain"])
}
, contentType("text/plain")
)
.route("/")
.method("GET"
, function () {
return request("http://me.iriscouch.com/db", {
json: true
})
}
, function () { return es.wait() }
, function () {
return es.mapSync(function(chunk) {
return "<html><head>cool</head><body>" + chunk.index +
"</body></html>"
})
}
, contentType("text/html")
)
;
http.createServer(handler, 8080)
function contentType(type) {
return function (req, res) {
return through(through.write, function () {
res.setHeader("Content-Type", type)
})
}
}