net::ERR_CONTENT_LENGTH_MISMATCH

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?

Author: Fantashit

5 thoughts on “net::ERR_CONTENT_LENGTH_MISMATCH

  1. 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.

  2. @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 with net::ERR_CONTENT_LENGTH_MISMATCH and never retry.

    The time for which the get call fails seems to depend on the hostname of the receiver. For localhost, 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 using wget with rate limiting (--limit-rate=5k) to imply a slow connection or large file that eventually takes too long to download.

    var fs = require('fs');
    var express = require('express');
    var wordsFile = '/usr/share/dict/words';
    var app = express();
    app.get('/send', (req, res) => res.send(fs.readFileSync(wordsFile)));
    app.get('/stream', (req, res) => fs.createReadStream(wordsFile).pipe(res));
    app.listen('3000');
    Calling Locally (succeeds)
    > wget 127.0.0.1:3000/send --limit-rate=3k
    
    --2017-08-16 12:17:30--  http://127.0.0.1:3000/send
    Connecting to 127.0.0.1:3000... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 938848 (917K) [application/octet-stream]
    Saving to: ‘send.3’ --- 916.84K  3.00KB/s    in 5m 6s   
    
    2017-08-16 12:22:35 (3.00 KB/s) - ‘send.3’ saved [938848/938848]
    Calling Externally (obfuscated IP; fails)
    > wget 8.8.8.8:3000/send --limit-rate=5k
    
    --2017-08-16 16:02:50--  http://8.8.8.8:3000/send
    Connecting to 8.8.8.8:3000... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 938848 (917K) [application/octet-stream]
    Saving to: “send.2” --- 64,882      5.00K/s   in 13s     
    
    2017-08-16 16:03:03 (5.00 KB/s) - Connection closed at byte 64882. Retrying.
    
    --2017-08-16 16:03:04--  (try: 2)  http://8.8.8.8:3000/send
    Connecting to 8.8.8.8:3000... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 938848 (917K) [application/octet-stream]
    Saving to: “send.2” --- 62,148      5.00K/s   in 12s     
    
    2017-08-16 16:03:16 (5.00 KB/s) - Connection closed at byte 64882. Retrying.
    
    --2017-08-16 16:03:18--  (try: 3)  http://8.8.8.8:3000/send
    Connecting to 8.8.8.8:3000... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 938848 (917K) [application/octet-stream]
    Saving to: “send.2” --- 62,148      5.00K/s   in 12s     
    
    2017-08-16 16:03:30 (5.00 KB/s) - Connection closed at byte 64882. Retrying.
    
    --2017-08-16 16:03:33--  (try: 4)  http://8.8.8.8:3000/send
    Connecting to 8.8.8.8:3000... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 938848 (917K) [application/octet-stream]
    Saving to: “send.2” --- 64,882      5.00K/s   in 13s     
    
    2017-08-16 16:03:46 (5.00 KB/s) - Connection closed at byte 64882. Retrying.

    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.

    var fs = require('fs');
    var express = require('express');
    var wordsFile = '/usr/share/dict/words';
    var app = express();
    // app.get('/send', (req, res) => res.send(fs.readFileSync(wordsFile)));
    app.get('/send', (req, res) => fs.createReadStream(wordsFile).pipe(res));
    app.listen('3000');
    Calling Externally (obfuscated IP)
    > wget 8.8.8.8:3000/send --limit-rate=5k
    
    --2017-08-16 16:24:42--  http://8.8.8.8:3000/send
    Connecting to 8.8.8.8:3000... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: unspecified
    Saving to: “send.3” --- 938,848     5.00K/s   in 3m 3s   
    
    2017-08-16 16:27:45 (5.00 KB/s) - “send.3” saved [938848]

    Using a streaming approach and setting the Content-Length header also seems to work, but it fails for Webpack Dev Server (not sure why)…

    var fs = require('fs');
    var express = require('express');
    var wordsFile = '/usr/share/dict/words';
    var wordsFileContentLength = 938848;
    var app = express();
    // app.get('/send', (req, res) => res.send(fs.readFileSync(wordsFile)));
    // app.get('/send', (req, res) => fs.createReadStream(wordsFile).pipe(res));
    app.get('/send', (req, res) => {
        res.header('Content-Length', wordsFileContentLength);
        fs.createReadStream(wordsFile).pipe(res);
    });
    app.listen('3000');
    Calling Externally (obfuscated IP)
    > wget 8.8.8.8:3000/send --limit-rate=5k
    --2017-08-16 16:29:29--  http://8.8.8.8:3000/send
    Connecting to 8.8.8.8:3000... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 938848 (917K)
    Saving to: “send.4” --- 938,848     5.00K/s   in 3m 3s   
    
    2017-08-16 16:32:33 (5.00 KB/s) - “send.4” saved [938848/938848]
  3. 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 like wget, 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.

    var fs = require('fs');
    var express = require('express');
    var wordsFile = '/usr/share/dict/words';
    var app = express();
    app.get('/send', (req, res) => {
        res.setHeader("Content-Type", "text/plain; charset=UTF-8");
        res.send(fs.readFileSync(wordsFile));
    });
    app.listen('3000');

    network1
    network2
    console1

Comments are closed.