Kaph is loose-coupled set of tools for handle requests under node.js. It's not framework.
*In the Phoenician alphabet letter "kaph" indicates palm.
Design of kaph was inspired by Connect, Tornado and many others. But unlike them, it was designed to make all components as independent from each other without large overhead.
You can install kaph as usual - by copy "kaph" directory in your
~/.node_libraries or via npm
npm install kaph
To handle request Kaph executes chain of defined operations.
var http = require('http');
var HttpHandler = require('kaph/http').Handler;
// Make some operations
OpA = {
DEFAULT: function() {
this.response.writeHead(200,
{'Content-Type': "text/html; charset=UTF-8"});
this.next();
}
};
OpB = {
GET: function(arg) {
this.response.end('OpB ' + arg);
},
ERROR: function(code, message) {
return code + ' ' + message;
}
};
var chain = [OpA, OpB]; // Our chain
var arg = ['Some arg']; // Optional arguments
http.createServer(function(request, response) {
// Just make new kaph handler and call the method "next"
(new HttpHandler(request, response, chain, arg)).next();
}).listen(9080);
Kaph handler receives four arguments:
requestis standart node.jshttp.ServerRequestresponseis standart node.jshttp.ServerResponsechainArrayof operations.argsOptionalArrayof arguments.
request and response may not be instances of node.js http module. They
simply must have the same behavior. Handler never interfere with their
implementation. It use only request.method property to choose operation
method (see below) as well as response's writeHead and end methods
on exceptions.
Now about chain. It's just Array of Objects. On each iteration kaph
handler invokes operation methods by following these rules:
- If operation has method with name matching
request.method, it performed as a native method of handler (yesapply) with optional arguments. - If operation hasn't named method, handler performs method with name
DEFAULT.
To invoke next operation in chain you must call next method of handler.
Kaph trying to handle all throwed exceptions without crash entire server. On
exception kaph handler ends request, logs error and try to send it to client.
To give a better description of exception you can throw new kaph.HandlerError
with two arguments: status code and optional message. Another way to throw
exception is handler's error method with same arguments.
By default kaph handler generates client error message with own kaph.ERROR
function. You can replace it with method ERROR of operation. ERROR method
also takes two code and message arguments and returns String.
Because handler performs the methods of operations as own, it's implementation is important. Each instance has next properties:
request... well, you understand :)response... well, it is also clear :)loggeris just node.jsconsole.
As stated in the beginning, kaph is not framework. But in directory chain
you find a few things that can make life much more pleasant:
Each of them can be used independently of kaph. And of course kaph, not depend on them.
I'm use my own daleth