Reputation: 1705
I am trying to create a simple web server in Node.js which would call Youtube video API to get a video and stream it to the client (after some checks, but that is not I am concerned about at the moment) as it retrieves it from Youtube.
To start with I am trying to do it in two parts, download and save to disk and then stream it from disk. While I am able to stream a file from disk to client, I could not get download and saving part working. Here is code for that
var request = require('request');
var http = require('http');
var fs = require('fs');
http.createServer(function(req,res)
{
request('http://www.youtube.com/embed/XGSy3_Czz8k').pipe(fs.createWriteStream('testvideo.mp4'));
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
I thought the request module could simply pipe output to file. Could someone please suggest what am I doing wrong here?
Any guidance on how to get the video from youtube and do a simultaneous streaming to the client while the video is being retrieved from youtube?
Upvotes: 8
Views: 10859
Reputation: 1705
I tried to do this and it works.
var request = require('request');
var http = require('http');
var fs = require('fs');
http.createServer(function(req,res)
{
var x = request('http://www.youtube.com/embed/XGSy3_Czz8k')
req.pipe(x)
x.pipe(res)
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Upvotes: 8