tinyservice
A tiny and lightweight microservice framework, created for nodejs. TinyService uses json-messages to exchange between server and client.
Install
npm install tinyservice --save
Server instance
Server instance of microservice, based on TCP-server.
constructor
const Server = Server;const serverInstance = port: 11234;
Parameters:
{Object} options
- required parameter to pass options to construct Server instance{number} options.port
- required parameter. Port, on which will be listen server
add
Adds handler to listen messages on server Params:
{Object<string, number|string|boolean>} pattern
- pattern to handle messages on server side. If incoming messages matches pattern - handler will be called with this message.{Function} handlerFunction
- function, that will be called if incomingMessage matches pattern. Function must accept two params:{Object} msg
- incoming message, that was received{Function} doneFunction
- function, that must be called after action was ended. Function receives two params - error and outgoing message. Error - if handler fails, Outgoing message - message to send to client as answer. CALL OF doneFunction is required!
Example:
serverInstance
remove
Removes handler by pattern.
{!Object} pattern
- required param. Pattern, on which you want to remove handler[{Function} handler]
- optional parameter. If not passed - all handlers will be removed[{boolean} strictMode = true]
- optional parameter. If passedtrue
- will be removed only actions, that fully matches passed pattern. If passedfalse
- will be removed common patterns.
Example:
server; server; server;
close
Closes server instance and unrefs port
Client instance
const Client = Client;const clientInstance = host: '127.0.0.1' port: 11234;
act
Emits message and handles answers from server Params:
{!Object} pattern
- required parameter. Pattern to send message to server.{Function} callback
- callback, that will be called, when server message will received by this call. EXAMPLE
serverInstance; serverInstance; clientInstance
close
Closes client instance