首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ytdl-core express下载视频的问题

使用ytdl-core express下载视频的问题
EN

Stack Overflow用户
提问于 2020-01-19 04:33:11
回答 1查看 683关注 0票数 0

我正试图构建一个简单的节点快速应用程序,使用ytdl从youtube下载视频,但出于某种原因,我的代码不起作用。我几乎什么都试过了,应用程序收到了请求,但没有做任何操作,页面似乎被刷新,但没有开始下载。

script.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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', (req, 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('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'public', '404.html'));
});
EN

回答 1

Stack Overflow用户

发布于 2020-09-22 21:02:09

我正面临着同样的问题,试着把这个放到你的script.js里,它对我有用。

代码语言:javascript
复制
window.location.replace(/*server url root*/ '/download?url=' + URLInput);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59807176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档