0.1.7 • Published 11 months ago

json-rpc2-fastify v0.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

json-rpc2-fastify

Simple json-rpc server over fastify and client over fetch for Node.js 18+

Protocol specification - https://www.jsonrpc.org/specification

Please see package usage examples in demo subdir.

Server

First import JsonRpcServer class and call the constructor with options JS object.

The options are:

  • port - the TCP-port number on which the HTTP-server will run, default is 4040 (if you do not set it when initializing the server).
  • entryPoint - the path where the server will listen for JSON-RPC requests via HTTP.
  • logger - request logging option that is passed to fastify server.
  • methods - an object whose properties define functions that can be called remotely via JSON-RPC protocol.

Code sample:

const JsonRpcServer = require('json-rpc2-fastify').JsonRpcServer;

const server = new JsonRpcServer({
    logger: true,
    port: 4040,
    entryPoint: 'jrpc',
    methods: {
        ping: async function (params) {
            const name = params?.name || 'Stranger';
            return {
                message: `Pong dear ${name}`,
            }
        },
        concat: async function (params) {
            const terms = params?.terms || [];
            const result = terms.join(' ');

            return result;
        }
    }
});

server.start();

The ping function just return an object with message to greet ${params.name}

0.1.7

11 months ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago