Call node.js request.end () method before configuring listeners

In nodejs docs http://nodejs.org/api/http.html#http_event_connect_1, the sample code calls request.end () before setting up the listeners (i.e. req.on (...) methods). The following is an example snipet code.

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

  req.on('connect', function(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', function(chunk) {
      console.log(chunk.toString());
    });
    socket.on('end', function() {
      proxy.close();
    });
  });

If this case does not end, the request ends immediately before setting the listeners, and listeners can never be called at all.

+4
source share
1 answer

req.end() , ( ), /. , http.request , .

, , - :

  • , ,
  • , ,

Next Run Loop Pass

  1. ..
+3

All Articles