zoServices
This Project is as implementation for JSON RPC v2.0 Spec written in Javascript for node.js, this helps for implements and usage methods that is in another server, system, programming languages or platform. This is Helpful for transport data without implements modules or program in other languages.
Install
Install the latest version of zoServices by executing npm install zoservices
in you shell.
Changelog
1.0.0
- Add promises to the project.
- Adding named variables for response.
- Fix Result as a correct form JSON RPC object.
- Fix Error as a correct form JSON RPC object.
- Multiple Server can be created at the same time.
- Servers can share the same response classes.
Usage
To start using this project just need add zoServices by adding require('zoservices');
to your project.
Server Options
Server.port
- Modify the port where server listen the request, by default the value of this options is 3000.
Server.defaultClass
- Define the main class when try to response a method, by default the value is main.
Server.responses_dir
- Define where server needs to look for the classes to responses, by default /responses/.
To set a config you will need the set
method and to retrieve a value from server you will need to use get
.
Response Classes
To start using Response classes you must create main
class into the responses
folder that you defined or the default settings.
Example of main class
{ var { settings = settings || {}; var self = {} selfresponse = {}; u; self{ return 'Hello World!!'; } self{ console; } self{ return 'a is ' + a + ' | b is ' + b + ' | c is ' + c; } return self; } $main = main;}typeof window === 'undefined' ? exports:window;
With this class server can response to the methods helloWorld
and paramMethod
and have a notification method doNotification
.
Making a Request
To make a request to the server is using the POST
method and sending well formed JSON string that contains the next value
/** Single Request **/ "id":<numeric|md5|timestamp> "method":"<methodName | className.methodName>" "jsonrpc":"2.0" "params":<arg1 arg2 argn | "argName1": argValue1 ... "argNamen":argValuen> /** Batch Request **/ "id":<numeric|md5|timestamp> "method":"<methodName | className.methodName>" "jsonrpc":"2.0" "params":<arg1 arg2 argn | "argName1": argValue1 ... "argNamen":argValuen> request
id
- This is the unique identification for request.method
- This is the method that it will call remotely you can usemethodName
to call the main class methods you can use thisclassName.methodName
to call a method from another defined class.jsonrpc
- this is the version of the protocol for the moment it just valid for jsonrpc 2.0 spec.params
- you can use an array of parameters or using a object with 'key:value' to assign a parameter.
Notification
In JSON RPC v2.0 Spec notification won't response anything to clients, zoServices will response with a empty response to indicates to client that server finish the request and is ready for another one.
Create a sigle server
var server = ; /** * Create a new Server **/var app = ; /** Asign to port 3001 **/app; /** Start server **/app;
Create Multiple Server with share classes.
var server = ; /** * Create a new Server **/var Server1 = ;var Server2 = ;var Server3 = ; /** This server share the same default response folder **/Server1;Server2; /** This server doesn't use the same classes **/Server3;Server3;