项目中用到 node调用hessian接口进行校验,一个npm的proxy-hessian就搞定,简单记录已备查找。
1、添加dependencies:"hessian-proxy"
2、使用
var ipAddress;
var forwardedIpsStr = req.header('x-forwarded-for');
if (forwardedIpsStr) {
var forwardedIps = forwardedIpsStr.split(',');
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
ipAddress = req.connection.remoteAddress;
}
console.log("ip地址:"+ipAddress)
//构造facade参数
var argForCredentialsRequestParam = {
systemCode:config.systemCode,
userId:name,
ip:ipAddress
};
console.log("facade所需参数:"+JSON.stringify(argForCredentialsRequestParam));
argForCredentialsRequestParam.__type__ = config.hermesParam;//参数传递给dto
var proxy = new Proxy(config.hermeshost, '', '', proxy);//指定调用hessian地址
proxy.invoke(config.hermesMethod,[argForCredentialsRequestParam], function (err, reply) {
if(err){
console.log("hessian调用出错"+err);
}
console.log('test: ' + JSON.stringify(reply));
res.json(JSON.stringify(reply));
});
具体详细参照npm官网:https://www.npmjs.com/package/hessian-proxy