This may be a somewhat stupid question but how would I pass a url into express url params array. When I try a youtube link half of it is cut off. For example 'https://www.youtube.com/watch?v=iik25wqIuFo' comes out as 'https://www.youtube.com/watch'. Thanks for any help. Heres my code:
const express = require('express');
const url = require('url');
const app = express();
const fs = require('fs')
const youtubedl = require('youtube-dl')
function downloadvideo(link) {
const video = youtubedl(link,
['--format=18'],
{ cwd: __dirname })
video.on('info', function(info) {
console.log('Download started')
console.log('filename: ' info._filename)
console.log('size: ' info.size)
})
video.pipe(fs.createWriteStream('myvideo.mp4'))
};
app.get('/', function (req, res) {
res.send('Hello Express app!');
});
app.get('/download/video/:id', function (req, res) {
app.use(express.urlencoded({ extended: true }));
res.send('Downloading ' req.params.id);
});
app.get('/download/subtitle/:id', function (req, res) {
res.send('user ' req.params.id);
});
app.get('/download/playlist/:id', function (req, res) {
res.send('user ' req.params.id);
});
app.listen(3000, () => {
console.log('server started');
});
CodePudding user response:
To pass in text with special characters like ?
, you must first run it through a function like encodeURIComponent()
which essentially makes these special characters' URL safe. So first step is to make the parameter URL safe by running it through encodeURIComponent()
(or you could escape special characters manually which is not fun):
console.log(encodeURIComponent('https://www.youtube.com/watch?v=iik25wqIuFo'));
// result: "https://www.youtube.com/watch?v=iik25wqIuFo"
Then pass it into your URL as a query parameter.
yourAppUrl.com/path?youtubeLink=https://www.youtube.com/watch?v=iik25wqIuFo