0

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');
});

1 Answer 1

1

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%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Diik25wqIuFo"

Then pass it into your URL as a query parameter.

yourAppUrl.com/path?youtubeLink=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Diik25wqIuFo
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.