@bifot/json-rpc
Solution for communication between services using HTTP protocol via JSON RPC. 🔬
Install
$ npm i @bifot/json-rpc -S
Usage
Server
const { Server } = require('@bifot/json-rpc');
const { Users } = require('../db');
const server = new Server();
server.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.error = 'Server error.';
}
});
server.on('get', async (ctx) => {
ctx.body = await Users.findOne({
id: ctx.params.id,
});
});
server.listen(process.env.APP_PORT);
Client
const { Client } = require('@bifot/json-rpc');
const client = new Client({
services: {
users: process.env.USERS_ADDRESS,
},
});
const response = await client.ask('users.get', {
id: 10,
});
console.log(response);