Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
express-json-rpc-router
Advanced tools
JSON-RPC official spec.
Extremely fast and simple Node.js JSON-RPCv2 router middleware. Handle incoming request and apply to controller functions. Validation available.
Note As Router require body-parser which must be used before router.
$ npm install express-json-rpc-router
or
$ yarn add express-json-rpc-router
const app = require('express')()
const bodyParser = require('body-parser')
const jsonRouter = require('express-json-rpc-router')
const controller = {
testMethod(req, res, next) {
// you can access to json rpc params via req.body
console.log('username: ', req.body.params)
return ['example data 1', 'example data 2']
}
}
app.use(bodyParser.json())
app.use(jsonRouter({ methods: controller }))
app.listen(3000, () => console.log('Example app listening on port 3000'))
and
curl -X POST \
http://localhost:3000 \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"method": "testMethod",
"params": {
"username": "valeron"
},
"id": 1
}'
will return:
{
"jsonrpc": "2.0",
"result": [
"example data 1",
"example data 2"
],
"id": 1
}
const controller = {
testMethod(req, res, next) {
// you can access to json rpc params via req.body
console.log('username: ', req.body.params)
return ['example data 1', 'example data 2']
}
}
const controllerValidation = {
testMethod() {
if (true) {
throw new Error('something going wrong')
}
}
}
app.use(bodyParser.json())
app.use(jsonRouter({
methods: controller,
beforeMethods: controllerValidation,
onError() {
console.log('Send report!')
}
}))
app.listen(3000, () => console.log('Example app listening on port 3000'))
and
curl -X POST \
http://localhost:3000 \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"method": "testMethod",
"params": {
"username": "valeron"
},
"id": 1
}'
will return:
{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "something going wrong"
},
"id": 1
}
The express-json-rpc-router
function takes an optional options
object that may contain any of the following keys:
You can pass the object of your methods that will be called when a match is made via JSON-RPC method
field.
Optionally you can pass the object of your validation methods, which will be called before main method with same name are called.
callback(err, req, res, next) {} Optionally you can pass onError callback which will be called when json-rpc middleware error occurred. First argument will be error and next default express arguments.
FAQs
Json-rpc middleware router for express
The npm package express-json-rpc-router receives a total of 49 weekly downloads. As such, express-json-rpc-router popularity was classified as not popular.
We found that express-json-rpc-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.