V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
coolicer
V2EX  ›  Node.js

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

  •  
  •   coolicer · Mar 9, 2015 · 4720 views
    This topic created in 4077 days ago, the information mentioned may be changed or developed.
    在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=UTF-8',
    '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
    Supplement 1  ·  Mar 9, 2015
    Got response: 500

    { 'cache-control': 'no-cache',
    pragma: 'no-cache',
    'content-type': 'application/json; charset=utf-8',
    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 replies    2015-03-09 20:47:58 +08:00
    typcn
        1
    typcn  
       Mar 9, 2015
    这东西为什么要用 node.js 做。。。 nginx 不行吗
    coolicer
        2
    coolicer  
    OP
       Mar 9, 2015
    @typcn 尝试一下,主要是mac装不了.net
    hcymk2
        3
    hcymk2  
       Mar 9, 2015
    你的asp.net 应用 500 了.
    jackmasa
        4
    jackmasa  
       Mar 9, 2015
    格式太乱了,发 gist 吧。
    coolicer
        5
    coolicer  
    OP
       Mar 9, 2015
    @jackmasa 我主要想知道options 参数有没有写错,提示500说明也是传了东西过去的
    coolicer
        7
    coolicer  
    OP
       Mar 9, 2015
    代码片
    <script src="https://gist.github.com/coolicer/880c617081503e69de70.js"></script>

    怎么直接贴上来不行
    finian
        8
    finian  
       Mar 9, 2015
    你自己 debug 一下不就知道哪里错了吗?
    shuson
        9
    shuson  
       Mar 9, 2015
    推荐使用
    https://github.com/nodejitsu/node-http-proxy

    你的code有问题,导致enobufs error, 看看这个
    https://github.com/joyent/node/issues/3269
    coolicer
        10
    coolicer  
    OP
       Mar 9, 2015
    @shuson 我知道哪错了。是参数那里
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5470 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 05:53 · PVG 13:53 · LAX 22:53 · JFK 01:53
    ♥ Do have faith in what you're doing.