Wednesday, 31 May 2017

Node.js: Sending stream data over server response(without pipe)

Instead of sending a read stream using pipe I want to send it by sending multiple parts of the file as chunks. The ultimate goal is going to be doing it over websocket, but I also want to get this down first. I was wondering how to use a streams data, end, close, readable events effectively. Here's my code.

const fs = require('fs');
const http = require('http');
const handler = (req, res) => {
  var stream = fs.createReadStream(req.url);
  stream.on('readable', () => {res.send(stream.read());});
};
const server = http.createServer(handler);
server.listen(80);

The ultimate goal, again, is to do it through websocket instead, but the problem it seems is in the way I handle the stream.



via Rey

No comments:

Post a Comment