我正在尝试构建一个简单的节点快速应用程序,以使用ytdl-core从youtube下载视频,但由于某些原因,我的代码无法正常工作。 我几乎尝试了所有操作,该应用程序收到了请求,但不执行任何操作,页面似乎已刷新,但没有下载开始。
script.js
var confirmText = "Downloading videos from YouTube is against the YouTube Policy.\n"
+ "The only videos that your allowed to download is your own.";
function downloadVideo() {
if (confirm(confirmText)){
var URLinput = document.getElementById("URL-input").value;
var req = new XMLHttpRequest();
req.open('get','/download?url=' + URLinput,false);
req.send();
}
}
index.js
const express = require('express');
const path = require('path');
const cors = require('cors');
const ytdl = require('ytdl-core');
const app = express();
app.use(cors());
app.use(express.static("public"));
app.listen(4000,() => {
console.log('Server Works !!! At port 4000');
});
app.get('/',(req,res) => {
res.sendFile(path.join(__dirname,'public','index.html'));
});
app.get('/download',res) => {
var url_input = req.query.url;
console.log(url_input);
res.header('Content-Disposition','attachment; filename="video.mp4"');
ytdl(url_input,{format: 'mp4'}).pipe(res);
});
app.get('*','404.html'));
});