A better approach is if you just connect the "data" to "write ()" and the "close" to "end ()":
// 0.3.x style fs.createReadStream(filename, { 'bufferSize': 4 * 1024 }).pipe(response) // 0.2.x style sys.pump(fs.createReadStream(filename, { 'bufferSize': 4 * 1024 }), response)
The read.pipe(write) or sys.pump(read, write) approach also has the advantage of flow control. That way, if the write stream cannot receive data as fast, it will say that the read stream will be turned off to minimize the amount of data that is buffered in memory.
flags:"r" and mode:0666 implied by the fact that this is a FileReadStream. The binary confirmation is outdated - if no encoding is specified, it will only work with raw data buffers.
In addition, you can add some other goodies that will make your file more complete:
- Sniff for
req.headers.range and see if the line matches the line /bytes=([0-9]+)-([0-9]+)/ . If so, you just want to go from this beginning to the end. (Invalid number means 0 or "end".) - The hash index and creation time from the stat () call to the ETag header. If you get a request header with "if-none-match" matching that header, send
304 Not Modified . - Check the
if-modified-since header for the mtime date of the mtime object. 304 if it has not been modified since the date of submission.
Also, in general, if you can, send a Content-Length header. (You put the file, so you should have this.)
isaacs Jan 04 2018-11-11T00: 00Z
source share