Node.js can be used to stream arbitrary data to a browser such as Chrome, over HTTP. In this post we’ll use latest version of the express middleware to stream a WebM file to the browser.
Execute the following npm command to install express
sudo npm install express@latest
npm installs express to a folder called node_modules, under the current folder. If you run node in the current folder, it should be able to find express.
Create a a file called webm.js with the following code
The commented headers in the response may be used for additional control. The
Transfer-Encoding header may also be
identity, its default value, as long as the
Connection response header is
Connection header is
Transfer-Encoding has to be
chunked. This behavior may be browser specific, I have only tested with Chrome. Chunking is taken care of by Node.js.
Running the code
To stream a WebM file at
/home/user/file.webm invoke node like
node webm.js 9001 /home/user/file.webm
Then, point Chrome to
http://host:9001/, and the video should begin playing.
Doing it the easy way
Now that we have seen the hard way, express has a method on the response object to send a file. It basically is a replacement for all the code that exists in the app.get() callback above: