用 nodejs 转发 http,帮我看看哪里错 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
coolicer
V2EX    Node.js

用 nodejs 转发 http,帮我看看哪里错

  •  
  •   coolicer 2015-03-09 11:20:02 +08:00 4579 次点击
    这是一个创建于 3945 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在cnodejs上发了,没人回答。各位大牛帮一下

    原帖 https://cnodejs.org/topic/54fd0d881e9291e16a7b34f5
    if(req.url == '/data/get' || req.url == 'data/get') {
    var post_data = querystring.stringify({
    proc: 'getConfig'
    });
    console.log('post_data->',post_data)
    var optiOns= {
    host: '192.168.1.246',
    port:80,
    path: '/data/get',
    method: 'POST',
    headers: {
    'Content-Type': 'application/json; charset=',
    'Content-Length': post_data.length
    }
    };
    var body = ''
    var client_req = http.request(options, function(res) {
    console.log("Got response: " + res.statusCode);
    res.on('data',function(d){
    body += d;
    }).on('end', function(){
    console.log(res.headers)
    console.log(body)
    });

    }).on('error', function(e) {
    console.log("Got error: " + e.message);
    })
    client_req.write(post_data)
    client_req.end();
    }

    我需要使用服务器的地址(192.168.1.246),把 localhost/data/get转发给192.168.1.246/data/get
    第 1 条附言    2015-03-09 12:39:56 +08:00
    Got response: 500

    { 'cache-control': 'no-cache',
    pragma: 'no-cache',
    'content-type': 'application/json; charset=',
    expires: '-1',
    server: 'Microsoft-IIS/7.5',
    'x-aspnet-version': '4.0.30319',
    'x-powered-by': 'ASP.NET',
    date: 'Mon, 09 Mar 2015 03:59:49 GMT',
    connection: 'close',
    'content-length': '36' }

    {"Message":"An error has occurred."}
    10 条回复    2015-03-09 20:47:58 +08:00
    typcn
        1
    typcn  
       2015-03-09 12:43:10 +08:00
    这东西为什么要用 node.js 做。。。 nginx 不行吗
    coolicer
        2
    coolicer  
    OP
       2015-03-09 12:55:38 +08:00
    @typcn 尝试一下,主要是mac装不了.net
    hcymk2
        3
    hcymk2  
       2015-03-09 13:14:39 +08:00
    你的asp.net 应用 500 了.
    jackmasa
        4
    jackmasa  
       2015-03-09 13:16:24 +08:00
    格式太乱了,发 gist 吧。
    coolicer
        5
    coolicer  
    OP
       2015-03-09 13:21:48 +08:00
    @jackmasa 我主要想知道options 参数有没有写错,提示500说明也是传了东西过去的
    coolicer
        6
    coolicer  
    OP
       2015-03-09 13:24:13 +08:00
    coolicer
        7
    coolicer  
    OP
       2015-03-09 13:25:14 +08:00
    代码片
    <script src="https://gist.github.com/coolicer/880c617081503e69de70.js"></script>

    怎么直接贴上来不行
    finian
        8
    finian  
       2015-03-09 14:46:00 +08:00
    你自己 debug 一下不就知道哪里错了吗?
    shuson
        9
    shuson  
       2015-03-09 16:39:40 +08:00
    推荐使用
    https://github.com/nodejitsu/node-http-proxy

    你的code有问题,导致enobufs error, 看看这个
    https://github.com/joyent/node/issues/3269
    coolicer
        10
    coolicer  
    OP
       2015-03-09 20:47:58 +08:00
    @shuson 我知道哪错了。是参数那里
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1503 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 16:32 PVG 00:32 LAX 08:32 JFK 11:32
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86