tjanczuk / iisnode

Hosting node.js applications in IIS on Windows
Other
1.86k stars 588 forks source link

Chunked encoding response not working #386

Open nickdima opened 10 years ago

nickdima commented 10 years ago

I have some trouble making chunked encoding work on an express app running on Azure Websites. This is the example code that I'm using:

app.all("/test", function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
  res.write('Hello\n');
  setTimeout(function() {
      res.end('World\n');
  }, 2000);  
});

And here are the response headers from calling this route with curl:

< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=utf-8
* Server Microsoft-IIS/8.0 is not blacklisted
< Server: Microsoft-IIS/8.0
< X-Powered-By: Express
< X-Powered-By: ASP.NET
< Date: Sun, 21 Sep 2014 11:32:10 GMT

Now the problem is that the response doesn't arrive in chunks but all at once. I disabled gzip compression both from IIS and node, but still no difference.

tjanczuk commented 10 years ago

Generally speaking the HTTP protocol does not guarantee that the client will receive the exact same chunks as the server produces. The behavior is dependent on the intermediate processing layers. In fact, in an extreme case a proxy may decide to buffer the entire response and send it back to the client without chunking at all.

In other words, your application logic cannot rely on chunks being preserved.

Having said that, by default IIS caches the HTTP response data up to 4MB before flushing - this is why you see the two chunks squashed into one. You can disable this buffering behavior (while loosing some of the performance benefits it offers) with this iisnode setting: https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/iisnode.yml#L130-L133. Note that this will only affect your server behavior. If there are any proxies between your client and server, the end result may depend on their behavior.

nickdima commented 10 years ago

Thanks for your quick reply @tjanczuk ! I'll give that a try. All I want is to split my generated html page in 2 or 3 chunks so the containing the css link gets sent upfront so it will be loaded while my server is still generating the page content.

thimp-topomorph commented 10 years ago

I have followed the instructions above, and placed the flushResponse attribute in webconfig: <iisnode flushResponse="true" /> Still, when I compare the result from piping a webcamera stream through node directly with the same application through iisnode, I can see IIS is buffering the piped chuncks. The webcam image is repeatedly frozen for about a minute, then rushes forward.

I am using IIS on a windows 8 machine, could that be a reason for the flushResponse configuration is not being effective, or do you have other suggestions to solve this. Im using iisnode 0.2.16 and IIS 8.5.9600.16384 Thanks, Arjan

EDIT: After deploying the app in IIS on a Windows Server 2008 the flushResponse attribute works OK, and the video is streamed evenly.

markusahlstrand commented 8 years ago

Had a similar issue when streaming audio using iisnode and it seems that you need to set the responsebuffer to 0 in web.config to make sure that iis doesn't keep a cache on the way out:

 <handlers>
            <add name="iisnode" path="app.js" verb="*" modules="iisnode" responseBufferLimit="0"/>
        </handlers>

According to ms-support the setting in iisnode does not disable the response buffer cache.

alekskorovin commented 4 years ago

Had a similar issue when streaming audio using iisnode and it seems that you need to set the responsebuffer to 0 in web.config to make sure that iis doesn't keep a cache on the way out:

 <handlers>
            <add name="iisnode" path="app.js" verb="*" modules="iisnode" responseBufferLimit="0"/>
        </handlers>

According to ms-support the setting in iisnode does not disable the response buffer cache.

That fixed my issue with the net::ERR_CONTENT_DECODING_FAILED error when requested through the proxy dynamic text file.