代码之家  ›  专栏  ›  技术社区  ›  Daksh

节点。JS http get请求返回未定义

  •  2
  • Daksh  · 技术社区  · 7 年前

    这本来应该很简单,但我没有得到数据回来,而是得到了未定义的。

    我在代理服务器后面,我不擅长从代理服务器后面发出请求。我发现只有一个教程解释了在不使用外部模块的情况下使用代理服务器时的http请求,但没有正确解释。

    const http = require('http');
    let options = {
        host: '10.7.0.1',
        port:8080,
        path:'www.google.co.uk'
    }
    
    http.get(options,(res) => {
        let str = '';
        res.setEncoding('utf8');
        res.on('data', (chunk) => {
        str += chunk;
    })
    
    res.on('end', (str) => {
        console.log(str);
        })
    })
    

    我甚至不知道这个方法是否正确,我也无法理解。我阅读了节点http文档,并尽我所能编写了options对象,如果我错了,请纠正我。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Ahmed Can Unbay    7 年前

    重要的

    const options = {
        port: 8080,
        hostname: '10.7.0.1',
        method: 'CONNECT',
        path: 'www.google.com:80'
      };
    
      const req = http.request(options);
      req.end();
    
      req.on('connect', (res, socket, head) => {
    console.log('got connected!');
    
    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();
    }
    

    额外信息 (不需要)

    这是我找到的完整代码 here

    const http = require('http');
    const net = require('net');
    const url = require('url');
    
    // Create an HTTP tunneling proxy
    const proxy = http.createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('okay');
    });
    proxy.on('connect', (req, cltSocket, head) => {
      // connect to an origin server
      const srvUrl = url.parse(`http://${req.url}`);
      const srvSocket = net.connect(srvUrl.port, srvUrl.hostname, () => {
        cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
                        'Proxy-agent: Node.js-Proxy\r\n' +
                        '\r\n');
        srvSocket.write(head);
        srvSocket.pipe(cltSocket);
        cltSocket.pipe(srvSocket);
      });
    });
    
    // now that proxy is running
    proxy.listen(1337, '127.0.0.1', () => {
    
      // make a request to a tunneling proxy
      const options = {
        port: 1337,
        hostname: '127.0.0.1',
        method: 'CONNECT',
        path: 'www.google.com:80'
      };
    
      const 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.createServer() 然后开始听 proxy.listen() connect 方法,并在套接字上写入一个简单的get请求,并按指定路径将其发送给google。

        2
  •  0
  •   Daksh    6 年前

    这个 request http 以一种非常优雅的方式请求,并且为了指定代理,只需要设置 proxy

    const request = require('request');
    
    request({url: 'http://example.com', proxy: 'https://host:port'}, (error, response, body) => {
      //Do whatever you want
    });