node.js调用基于java的hessian服务方法(包含获取客户端IP)

2015-03-29  文晶 

项目中用到 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


1079°/10783 人阅读/1 条评论 发表评论

超沙坡  2015-10-06

请问,传递中文有没出现乱码?


登录 后发表评论