title: node.js http.request event flow - where did my END event go?
body: <p>I am working on a cunning plan that involves using node.js as a proxy server in front of another service. </p>
  
  <p>In short:</p>
  
  <ol>
  <li>Dispatch incoming request to a static file (if it exists)</li>
  <li>Otherwise, dispatch the request to another service</li>
  </ol>
  
  <p>I have the basics working, but now attempting to get the whole thing working with Sencha <a href="http://senchalabs.github.com/connect/" rel="nofollow">Connect</a> so I can access all the kick-ass middleware provided. </p>
  
  <p>All of the action happens in <em>dispatchProxy</em> below</p>
  
  <pre><code>connect(
    connect.logger(), 
    connect.static(__dirname + '/public'),
    (request, response) -&gt;  
      dispatchProxy(request, response)
  ).listen(8000)
  
  dispatchProxy = (request, response) -&gt;  
  
    options = {host: host, port: port, method: request.method, headers: request.headers, path: request.url}
  
    proxyRequest = http.request(options, (proxyResponse) -&gt;
      proxyResponse.on('data', (chunk) -&gt;
       response.write(chunk, 'binary')
      )
  
      proxyResponse.on('end', (chunk) -&gt;        
       response.end()
      )
  
      response.writeHead proxyResponse.statusCode, proxyResponse.headers    
    )
  
    request.on('data', (chunk) -&gt;
      proxyRequest.write(chunk, 'binary')
    )
  
    # this is never triggered for GETs
    request.on('end', -&gt;
      proxyRequest.end()
    )
  
    # so I have to have this here
    proxyRequest.end()
  </code></pre>
  
  <p>You will notice <strong><em>proxyRequest.end()</em></strong> on the final line above.</p>
  
  <p>What I have found is that when handling GET requests, the END event of the request is never triggered and therefore a call to proxyRequest.end() is required. POST requests trigger both DATA and END events as expected.</p>
  
  <p>So several questions:</p>
  
  <ul>
  <li><p>Is this call to proxyRequest.end() safe? That is, will the proxyResponse still be completed even if this is called outside of the event loops?</p></li>
  <li><p>Is it normal for GET to not trigger END events, or is the END being captured somewhere in the connect stack?</p></li>
  </ul>