Skip to content

josp70/jsonrpc2-express

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

jsonrpc endpoint for express

Resources

License

Stars

Watchers

Forks

Packages

No packages published