简体   繁体   English

Nodejs:应用标头并获得响应

[英]Nodejs: apply headers and get response

I have the following GET request:我有以下 GET 请求:

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.我想在 node.js 中得到响应。 Can 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 .一个起点是 http.request 的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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM