Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 点头。如何通过http连接隧道发送http请求?_Node.js_Sockets - Fatal编程技术网

Node.js 点头。如何通过http连接隧道发送http请求?

Node.js 点头。如何通过http连接隧道发送http请求?,node.js,sockets,Node.js,Sockets,这里有一个使用node.js文档中的HTTP连接隧道的示例 var options = { port: 1337, hostname: '127.0.0.1', method: 'CONNECT', path: 'www.google.com:80' }; var req = http.request(options); req.end(); req.on('connect', (res, socket, head) => {

这里有一个使用node.js文档中的HTTP连接隧道的示例

  var options = {
    port: 1337,
    hostname: '127.0.0.1',
    method: 'CONNECT',
    path: 'www.google.com:80'
  };

  var req = http.request(options);
  req.end();

  req.on('connect', (res, socket, head) => {
    console.log('got connected!');

    // make a request over an HTTP tunnel
    socket.write('GET / HTTP/1.1\r\n' +
                 'Host: www.google.com:80\r\n' +
                 'Connection: close\r\n' +
                 '\r\n');
    socket.on('data', (chunk) => {
      console.log(chunk.toString());
    });
    socket.on('end', () => {
      proxy.close();
    });
  });

有没有一种方法可以使用带有http库的已建立隧道的套接字,而不是自己通过套接字传递http请求体(Tour socket.write)?

我认为常规的
http
库没有任何代理支持,所以您必须自己实现它

该软件包(构建在
http
之上)将使事情变得更简单,不过:

const request = require('request');

request({
  url    : 'http://www.google.com',
  proxy  : 'http://localhost:1337'
  tunnel : true
}, (err, res, body) => {
  ...
});
通过指定,它还将隧道普通HTTP请求