проблема с загрузкой видео с ytdl-core express - PullRequest
0 голосов
/ 19 января 2020

Я пытаюсь создать приложение простого узла express для загрузки видео с YouTube с помощью ytdl-core, но по какой-то причине мой код не работает. Я перепробовал почти все, приложение получает запрос, но ничего не делает с ним, кажется, что страница обновляется, но загрузка не начинается.

скрипт. 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', (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'));
});
...