Node.JS Express Project to Get YouTube Channel & Video ID From URL in Browser Using Javascript

You are currently viewing Node.JS Express Project to Get YouTube Channel & Video ID From URL in Browser Using Javascript

Node.js Express Project to Get Youtube Channel & Video ID From URL in Browser Using Javascript

<span class="pl-c1">npm install @gonetone/get-youtube-id-by-url</span>

<span class="pl-k">const</span> <span class="pl-kos">{</span> channelId<span class="pl-kos">,</span> videoId <span class="pl-kos">}</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'@gonetone/get-youtube-id-by-url'</span><span class="pl-kos">)</span>

<span class="pl-en">channelId</span><span class="pl-kos">(</span><span class="pl-s">'YouTube Channel Url'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">then</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-s1">id</span><span class="pl-kos">)</span> <span class="pl-c1">=></span> <span class="pl-kos">{</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">id</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-en">videoId</span><span class="pl-kos">(</span><span class="pl-s">'YouTube Video Url'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">then</span><span class="pl-kos">(</span><span class="pl-kos">(</span><span class="pl-s1">id</span><span class="pl-kos">)</span> <span class="pl-c1">=></span> <span class="pl-kos">{</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">id</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>

const express = require('express')
 
const { channelId, videoId } = require("@gonetone/get-youtube-id-by-url");
 
const bodyparser = require('body-parser')
 
const app = express()
 
app.use(bodyparser.urlencoded({ extended: false }))
 
app.use(bodyparser.json())
 
app.set('view engine', 'ejs')
 
app.get('/', (req, res) => {
    res.render('index',{channelid:'',videoid:''})
})
 
app.post('/getchannelid', (req, res) => {
    channelId(req.body.channelurl).then((id) => {
        // pass this id
 
        res.render('index',{channelid:id,videoid:''})
    })
})
 
app.post("/getvideoid", (req, res) => {
  videoId(req.body.videourl).then((id) => {
    // pass this id
 
    res.render("index", { channelid: '', videoid: id });
  });
});
 
 
app.listen(5000, () => {
    console.log("App is listening on port 5000")
})

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Get Channel id and video id</title>
</head>
<body>
    <form action="/getchannelid" method="post">
        <label for="Enter channel URL"></label>
        <input type="text" name="channelurl" id="" required>
        <input type="submit" value="Get Channel Id">
    </form>
    <br><br>
 
    <% if(channelid) {%>
 
        <h1>Your Channel ID is: <%=channelid%></h1>
 
    <%}%>
 
 
     <form action="/getvideoid" method="post">
        <label for="Enter Video URL"></label>
        <input type="text" name="videourl" id="" required>
        <input type="submit" value="Get Video Id">
    </form>
 
       <% if(videoid) {%>
 
        <h1>Your Video ID is: <%=videoid%></h1>
 
    <%}%>
</body>
</html>

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply