cannot pipe mp4 file in node.js

I am trying to pipe an mp4 file in my root directory to but I cannot get it to work, it start buffering for streaming but never plays the file.

here is my code

var http = require('http'),
    server = http.createServer(),
    fs = require('fs'),
    rs = fs.createReadStream('./Practice/Jumanji.mp4');
server.on('request', function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'video/mp4'
    });
    rs.pipe(res);
    res.end();
});
server.listen(4000);

The movie tries to load but never does.

You have two problems in your code that I see

  1. Streams are one-time use objects, so once you fetch one video, a second request would fail because the stream was already closed. Your rs = fs.createReadStream('./Practice/Jumanji.mp4'); line should be inside the callback.
  2. Likely the cause of your error in this case, is res.end();. That will immediately close res before your pipe has had time to write the video, so you are essentially saying "Send this stream" followed immediately by "Send nothing". You should delete that line, since the pipe will automatically close res when all the data is written.

All this said, there is a lot of error handling logic that your example is missing, which you may want to consider.