I have code for reading an image from directory and sending it to index.html.
I am trying to replace fs.readFile with fs.createReadStream but i have no idea how to implement this as i can not find a good example.
Here is what i got (index.js)
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
http.listen(3000, function () {
console.log('listening on *:3000');
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/views/index.html');
});
io.on('connection', function (socket) {
fs.readFile(__dirname + '/public/images/image.png', function (err, buf){
socket.emit('image', { image: true, buffer: buf.toString('base64') });
});
});
index.html
<!DOCTYPE html>
<html>
<body>
<canvas id="canvas" width="200" height="100">
Your browser does not support the HTML5 canvas tag.
</canvas>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
var socket = io();
var ctx = document.getElementById('canvas').getContext('2d');
socket.on("image", function (info) {
if (info.image) {
var img = new Image();
img.src = 'data:image/jpeg;base64,' + info.buffer;
ctx.drawImage(img, 0, 0);
}
});
</script>
</body >
</html >
The below approach only uses core modules and reads the chunks from the
stream.Readable
instance returned fromfs.createReadStream()
and returns those chunks back as aBuffer
. 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 aBuffer
which resides in memory, so its only a good solution for reasonably sized files.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 canpipe()
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()
astream.Readable
to ahttp.ServerResponse
. the example uses Express but it works the exact same way usinghttp
orhttps
from the Node.js Core API.