The jsonrpc middleware provides JSON-RPC 2.0 support. Below is an example exposing the add and sub methods:
var math = {
add: function(a, b){
try {
this(null, a + b);
} catch (err) {
this(err);
}
},
sub: function(a, b){
try {
this(null, a - b);
} catch (err) {
this(err);
}
}
};
var date = {
time: function(){
this(null, new Date().toUTCString());
}
};
connect.createServer(
connect.jsonrpc(math, date)
);
The value of this becomes the async callback function. When you wish to pass an exception simply invoke this(err)
, or pass the error code this(jsonrpc.INVALID_PARAMS)
. Other this(null, result)
will respond with the given results.