开发者

Nodejs: apply headers and get response

I have the following GET request:

GET http://www.google.ie/ HTTP/1.1
Host: www.google.ie
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive
Cookie: PREF=ID=0000043ea43e2426:U=204008a193b06a93:FF=0:TM=1310983818:LM=1310983985:S=HhQ3hzHoRpfrsFN4; NID=50=bT7R608p1asdflr9QiJ_cY80WjaFZ6cB-IJGLT6rpSdiH6bQwnxAEDGTJ1k4K3-A4Y6327iyepbXL6d3fnomtBcWXPQ7A5Px1zckZGBoo8gtMrixSGneodtc7IIaxSu; SID=DQAAALcAAACa0eOu2S9ezDasdfx32stdYzKQQCc7Q4dcYucZkXOaQkXKmfkr0iMlPQZkwy4PlQLzZsiO_5_lLDclyBDJsJIKU0my000owlYMX14K22pBopTN1EUlOrJ7LIkwhznasdfBleSojFfhMbn0BoYM1WAzwnpMAttoAuzG0bZXcScgZkDizC2FUHXVV3-eHZPrS2ncychNguPNZ_M9V_oEtoqJUmqasdf_kaKTOM2KnT0P5wMswKru8_KrkwK6iCc7ag; HSID=A78ACtAr9H6MYp-dn
Cache-Control: max-age=0

I want to get the reponse in node.js. C开发者_如何学JAVAan someone please point me in the right direction as to how I might do this?

Many thanks in advance,


One place to start is the http module docs for http.request.


Is it, proxy? If so, then you can use such proxy:

var net = require('net');

// Create TCP-server
var server = net.createServer( function(soc){ // soc is socket generated by net.Server

  // Incoming request processing
  soc.on('data', function(data){


    // Create new socket
    var client = net.Socket();

    // Get host from HTTP headers
    var re = /[^\n]+(\r\n|\r|\n)host:\w*([^:\r\n]+)/i;
    var host = data.toString('utf-8').match(re);

    // Pause soc for inner socket connection
    soc.pause();

    // Connect to Node.js application
    client.connect(80, host);

    client.on('connect', function()
    {
      // Write request to your node.js application
      client.write(data.toString('utf-8'));
    });

    client.on('data', function(cdata)
    {
      // Return socket to live
      soc.resume();

      // Write client data to browser
      soc.write(cdata.toString('utf-8'));
      soc.pipe(soc);
      soc.end();
    });

    client.on('end', function(){
      client.destroy();
    });
  });
}
);

server.on('error', function (err){
  // Error processing i just pass whole object
  console.log(err);
});

server.listen(8088);
console.log('Server is listening %d\n', 8088);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜