I am writing a simple Node.js server to serve videos from my laptop into my local network.
I have been searching around, to check the best way, and I found two ways of doing it, I would like some help figuring out if there is one way that is preferred over the other when it comes to serve video files using nodejs.
Video files would be huge video files, movie size....
Option 1: using Express:
I have seen some people that, after creating all the usual boilerplate code for an Express server, when it comes to sending the video, they would just do something like:
app.get('/getMovie', (req, res) => {
res.sendFile('assets/video.mp4', { root: __dirname });
});
Option 2: using Http:
For this option, after creating the http server and so on, people would create first a ReadStream using fs
and then send the video. Something like:
res.writeHead(200, {'Content-Type': 'video/mp4'});
let readStream = fs.createReadStream("movie.mp4");
readStream.pipe(res);
For me, option 2 sounds more reasonable when it comes to huge video files, but, not sure which one would be best and why.