-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
52 lines (43 loc) · 1.52 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
// required packages
const express = require('express');
const fetch = require('node-fetch');
require('dotenv').config();
// creating the express server
const app = express();
// server port number
const PORT = process.env.PORT || 3000;
// set template engine
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use('/assets', express.static("public"))
// needed to parse html data POST request
app.use(express.urlencoded({
extended: true
}))
app.use(express.json());
app.get('/', (req, res) => {
res.render('index');
})
app.post('/convert-mp3', async (req, res) => {
const videoID = req.body.videoID;
if (videoID === undefined || videoID === '' || videoID === null) {
return res.render('index', { success: false, message: 'Please enter a video ID' });
} 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();
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, song_title: fetchResponse.title, message: fetchResponse.msg });
}
})
// starting the server
app.listen(PORT, () => {
console.log('Server on port 3000')
});