I use express.js(version 4.15.3) with node 8.1.2.
I try to print a 10-mb object in json:
import express=require('express');
function handler=async (req,res)=>
{
try{
let largeObject=await someFunctionThatReturnsLargeObject(); //just for the example
message=(JSON.stringify(largeObject));
}
catch(e)
{
//has been debugged, it never gets here. it prints an error.
}
finally
{
res.end(message);
}
}
I use typescript 2.3
sometimes when I call for a request from this handler, only part of the content is printed, and I get net::ERR_CONTENT_LENGTH_MISMATCH.
what do I do wrong?
I’ve moved to node 8.3.0.
it seems that end collapses during printing, for some reason because only part of the print is made, and it is shorter than the header.
it usually occurs when an output of 8MB is sent.
@dougwilson I’m seeing this same problem and trying to resolve an issue with Webpack Dev Server and it’s middleware, that @shellscape has referenced. Using
wget
, large files or files on a slow connection are receiving a 206 – Partial Content.wget
will retry them, failing over and over again. Within the browser, Chrome will flag the download as failed withnet::ERR_CONTENT_LENGTH_MISMATCH
and never retry.The time for which the
get
call fails seems to depend on the hostname of the receiver. Forlocalhost
,0.0.0.0
,127.0.0.1
, it seems to fail at about 4 minutes. When calling the dev-server application externally, from a completely different IP, it seems to fail at about 5-20 seconds.To address this as an Express only bug:
Note: I’d like to point out a difference between these tests and Webpack Dev Server. The Dev Server ends with a 206 – Partial Content, but in the examples below, I’m only seeing 200’s. Secondly, it seems that the 4 minute mark is passed in the examples below, while Webpack Dev Server is failing exactly at 3 minute 58 seconds (locally). Lastly, using the Streaming approach but setting the
Content -Length
header seems to work here, but not for Webpack Dev Server…It seems the Express versions are the same between my tests and their’s, so I’m not sure why there are inconsistencies (maybe setting mime type or handing ranges?).
Using Express 4.15, use
res.send
to ship a file. I’m usingwget
with rate limiting (--limit-rate=5k
) to imply a slow connection or large file that eventually takes too long to download.Calling Locally (succeeds)
Calling Externally (obfuscated IP; fails)
Using a streaming approach seems to work
Using
fs.createReadStream
and piping that into the response does seem to work, all the time. It’s just that the client doesn’t know what the Content-Length is.Calling Externally (obfuscated IP)
Using a streaming approach and setting the
Content-Length
header also seems to work, but it fails for Webpack Dev Server (not sure why)…Calling Externally (obfuscated IP)
Node version is 8.4.0 (also have seen this with 8.3.0)
Per the
ERR_CONTENT_LENGTH_MISMATCH
question, the Webpack Dev Server (and in my response here, Express explicitly) fails to send the full content of a JavaScript file to an external IP address. Using a CLI tool likewget
, Webpack Dev Server shows it receives a 206 status code for Partial Content. In the examples I have above, it only shows a 200.But using a browser, like Chrome, it fails the request completely and spits back that specific error
net::ERR_CONTENT_LENGTH_MISMATCH
.I set Chrome to throttle the request to 2G so it slows the download to simulate a large file or slow connection.
I ran into this issue using both Express and Nginx.
This SO post helps me. I fixed the problem by setting
in nginx conf of my site.