json-rpc-lite
v2.0.8
Published
json-rpc-lite
Downloads
20
Readme
json-rpc-lite
very simple jsonrpc base on express
Install
npm i json-rpc-lite -S
Usage
server
加载处理模块
const JSONRPC = require('json-rpc-lite');
JSONRPC.loadRouter(path.join(__dirname, 'rpc/')); // 自动加载目录中 xxx_rpc.js文件 xxx为模块名 文件中 函数名为yyyRpc,其中yyy为导出的模块方法,非此格式函数 无法rpc调用
example:connector_rpc.js
var service = module.exports = {
init: function () {
logger.debug('[connector_prc]init');
},
kickRpc: function (input, cb) {
logger.debug('[rpc]kick:%j', input);
let checkParam = validate(input, jsonSchemaData.kick_rpc);
if (!checkParam.valid) {
let errMsg = checkParam.errors[0].message;
cb(new Error(errMsg));
return;
}
cb(null, {});
return;
}
}
input: argument cb: callback(error, returnValue)
route
baseon express
var inputSchema = {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"module": {
"type": "string"
},
"method": {
"type": "string"
},
"args": {
"type": "object",
}
},
"required": ["id", "module", "method", "args"]
};
var service = module.exports = {
invokeAction: {
method: ['POST'],
handler: function (req, res) {
logger.debug('[rpc]invoke:%j', req.body);
var checkParam = validate(req.body, inputSchema);
if (!checkParam.valid) {
let errMsg = checkParam.errors[0].message;
logger.error('[rpc]invoke param error:%s', errMsg);
res.json(commonUtil.json(-1, errMsg));
return;
}
try {
JSONRPC.handlePOST(req, res);
} catch (error) {
logger.error('[rpc]call fatal:', error);
res.json({
status: -1,
msg: 'fatal error'
});
}
return;
}
}
};
client
jsonrpcClientPool.invokeWithHost('http://127.0.0.1:8011/api/rpc/invoke', 'connector', 'kick', {
echo: 'hello'
}).then(function(resp) {
logger.debug('logid:%s [rpc]kick remote success:%j', logid, resp);
return Promise.resolve();
}).catch(function(error) {
logger.error('logid:%s [rpc]kick remote failed:', logid, error.stack);
return Promise.resolve();
});
增加debug_server 只能目标地址 以渠道invokeWithHost
JsonRpc.invokeWithRalV2('dlpProxyRpcMock', {
debug_server: {
'host': host.ip,
port: host.port
}
},
'connector',
'ping',
postBody,
this.tracer.logid
)
See example.
License
The MIT License