Ring Middleware
Ring spec middleware flow:
Request
|
V
------------------ -------------- --------------
| Authentication | --> | Caching | --> | Controller |
| middleware | <-- | middleware | <-- | action |
------------------ -------------- --------------
|
V
Response
Example
var Rx = require("rx");
var RxHttpServer = require("rx-http-server");
var preAction = function(next) {
return function(data) {
data.result = "[pre] ";
return Rx.Observable.returnValue(data).flatMap(next);
};
};
var postAction = function(next) {
return function(data) {
return next(data).map(function(n) {
n.result += " [post]"
return n;
});
};
};
var action = function(data) {
data.result += "hello";
return Rx.Observable.returnValue(data);
};
var server = new RxHttpServer();
var handler = ringMiddleware(action, [
preAction,
postAction
]);
server.requests.flatMap(handler).subscribe(function(data) {
data.response.writeHead(200, {"Content-Type": "text/plain"});
data.response.end(data.result);
});
server.listen(3000);