-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (63 loc) · 1.68 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Required packages
import express from "express";
import fetch from "node-fetch";
import dotenv from "dotenv";
dotenv.config();
// Create express server
const app = express();
// Indicate the port number server will run on
const PORT = process.env.PORT || 3000;
// Set template engine
app.set("view engine", "ejs");
app.use(express.static("public"));
// Needed to parse html data for POST requests
app.use(
express.urlencoded({
extended: true,
})
);
app.use(express.json());
// GET route
app.get("/", (req, res) => {
res.render("index");
});
// POST route
app.post("/convert-mp3", async (req, res) => {
const videoURL = req.body.videoURL;
const indexOfEqual = videoURL.indexOf("=");
const videoId = videoURL.substr(indexOfEqual + 1, 11);
if (videoId === undefined || videoId === "" || videoId === null) {
return res.render("index", {
success: false,
message: "Please enter a youtube video URL",
});
} else {
const fetchAPI = await fetch(
`https://youtube-mp36.p.rapidapi.com/dl?id=${videoId}`,
{
method: "GET",
headers: {
"x-rapidapi-key": process.env.API_KEY,
"x-rapidapi-host": process.env.API_HOST,
},
}
);
const fetchResponse = await fetchAPI.json();
console.log(fetchResponse);
if (fetchResponse.status === "ok")
return res.render("index", {
success: true,
song_title: fetchResponse.title,
song_link: fetchResponse.link,
});
else
return res.render("index", {
success: false,
message: fetchResponse.message,
});
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});