Replacing fs.readFile with fs.createReadStream in Node.js Replacing fs.readFile with fs.createReadStream in Node.js express express

Replacing fs.readFile with fs.createReadStream in Node.js


The below approach only uses core modules and reads the chunks from the stream.Readable instance returned from fs.createReadStream() and returns those chunks back as a Buffer. This isn't that great of an approach if you're not going to stream the chunks back. You're going to hold the file within a Buffer which resides in memory, so its only a good solution for reasonably sized files.

io.on('connection', function (socket) {  fileToBuffer(__dirname + '/public/images/image.png', (err, imageBuffer) => {    if (err) {       socket.emit('error', err)    } else {      socket.emit('image', { image: true, buffer: imageBuffer.toString('base64') });     }  });});const fileToBuffer = (filename, cb) => {    let readStream = fs.createReadStream(filename);    let chunks = [];    // Handle any errors while reading    readStream.on('error', err => {        // handle error        // File could not be read        return cb(err);    });    // Listen for data    readStream.on('data', chunk => {        chunks.push(chunk);    });    // File is done being read    readStream.on('close', () => {        // Create a buffer of the image from the stream        return cb(null, Buffer.concat(chunks));    });}

HTTP Response Stream Example

Its almost always a better idea to use HTTP for streaming data since its built into the protocol and you'd never need to load the data into memory all at once since you can pipe() the file stream directly to the response.

This is a very basic example without the bells and whistles and just is to demonstrate how to pipe() a stream.Readable to a http.ServerResponse. the example uses Express but it works the exact same way using http or https from the Node.js Core API.

const express = require('express');const fs = require('fs');const server = express();const port = process.env.PORT || 1337;server.get ('/image', (req, res) => {    let readStream = fs.createReadStream(__dirname + '/public/images/image.png')    // When the stream is done being read, end the response    readStream.on('close', () => {        res.end()    })    // Stream chunks to response    readStream.pipe(res)});server.listen(port, () => {    console.log(`Listening on ${port}`);});