http rpc
rpc over http using streams
Build both the client and server from a common interface.
Install
npm install --save lib-http-rpc
Usage
Common
Specify a common API between the client and server.
var iface = getUser: method : 'GET' route : '/user/:name' setUser: method : 'POST' route : '/user/:name' listUsers: method : 'GET' route : '/users' options: limit: 10 order: 'desc' ;
Create an rpc
from your interface.
var RPC = ;var rpc = RPC;
Server
Define your server application without any http
dependencies.
var solidify = ;var liquify = ; { thisusers = {};} Appprototype { var name = paramsname; var user = thisusersname; // this causes an HTTP 404 error if !user throw 404 'user not found'; stream;}; Appprototype { var name = paramsname; var users = thisusers; // parse incoming stream as json ;}; Appprototype { var limit = paramslimit; var order = paramsorder; var users = Object; // sort and limit users users = ; // turn user object into a stream ;};
Generate a router from your application.
var router = rpc;;
Client
Generate a client based on the shared interface.
var client = rpc;var stream = client; stream;