jsonrpc2-express

0.4.4 • Public • Published

jsonrpc2-express: JSON-RPC end-point for express

build status

Module to configure an express router in order to expose a JSON-RPC end-point.

Installation

npm install jsonrpc2-express

Usage

const express = require('express');
const jsonrpc = require('jsonrpc2-express')
 
const app = express();
 
let routerRpc = express.Router();
 
jsonrpc('/rpc/module1', routerRpc, {
    methods: require('./module1.js'),
    bodyParser: {
        limit: '50mb'
    }
});
 
app.use('/api', routerRpc);
 
app.listen(3000, function() {
    console.log('Server listening on port 3000');
});

The file module1.js with the implementation of the different methods is:

exports.f1 = function(req) {
    console.log(req.body.params)
    return {
    field1: 'answer from f1',
    user: req.user
    };
};
 
exports.f2 = function(req) {
    console.log(req.body.params)
    return {
    field1: 'answer from f2',
    user: req.user
    };
};
 
exports.ferror = function(req) {
    throw new Error("sorry");
};

The parameters for each method can be read from req.body.params

Tests

npm test

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Package Sidebar

Install

npm i jsonrpc2-express

Weekly Downloads

1

Version

0.4.4

License

MIT

Unpacked Size

26.2 kB

Total Files

9

Last publish

Collaborators

  • josp.jorge