cloudhead / node-static

rfc 2616 compliant HTTP static-file server module, with built-in caching.
MIT License
2.17k stars 245 forks source link

Error when processing range request using Chrome #217

Open tim-kuteev opened 4 years ago

tim-kuteev commented 4 years ago

I get an error when request a .mp4 file using Chrome and Express:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
  at node-static.js:125:17

My code:

const express = require('express');
const StaticServer = require('node-static').Server;

const app = express();
const fileServer = new StaticServer(__dirname + '/public');

app.use((req, res, next) => {
  req.addListener('end', () => {
    fileServer.serve(req, res);
  }).resume();
});

app.listen(88, () => {
  console.log('Listening');
});

process.on('uncaughtException', (err) => {
  console.error(err);
});

I personally do not quite understand the meaning of this condition: https://github.com/cloudhead/node-static/blob/e59fe21dffbee46678362d26d26fdfb241f49506/lib/node-static.js#L124 so in case of the range request the status is set to 206 https://github.com/cloudhead/node-static/blob/e59fe21dffbee46678362d26d26fdfb241f49506/lib/node-static.js#L284 and it will writeHead and end response twice then.

I beleve, at least the condition at 124 should probably be status >= 300, though I could misunderstand the meaning of it.

Environment: node-static v0.7.11 node v12.13.0 express 4.17.1 chrome v77.0.3865.120 windows 10