First of all this one does not work for me. https://github.com/expressjs/compression/issues/56
router.get("/", (req, res) => {
const head = {
'Content-Length': 1905484,
'Content-Type': 'video/mp4'
};
res.writeHead(200, head);
for(let index=0; index<=finalChunkNumber; index++) {
let buf = getVideoBuffersFromServerSync(index);
res.write(buf);
res.flush();
}
res.status(200).send();
}
getVideoBuffersFromServerSync()
simply fetches buffer from a server and on the server .mp4 file read from a file stream. With this app, video is not displayed in the browser, until all buffers are fetched and res.send() is reached. Once res.send()
is reached browser displays video well. Also tried adding app.use(compression({ level: 0 }))
in express routes. Express does real-time streaming if it is reading from a file using fs.createReadStream
.
So how to make express to write buffers as a real-time response or is there any other HTTP server framework which can achieve these results?
Edit1: Based on jfriend00's comments added range part to the route as follows
const range = req.headers.range;
if(range) {
const fileSize = 1905484;
const parts = range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0], 10) ;
const end = parts[1]
? parseInt(parts[1], 10)
: fileSize-1 ;
const chunksize = (end-start)+1;
const index = start/chunksize;
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4'
};
let buf = getVideoBuffersFromServerSync(index);
res.writeHead(206,head);
res.write(buf);
res.flush();
}
This one makes first partial request but doesn't make subsequent requests. Any reason why? Do I need to use send()
instead of write()
?