-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (54 loc) · 1.44 KB
/
index.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
// Load Node modules
var express = require('express');
const stream = require('youtube-audio-stream')
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
// Initialise Express
var app = express();
// Render static files
app.use(express.static('public'));
// Set the view engine to ejs
app.set('view engine', 'ejs');
var port = process.env.PORT || 8080;
app.listen(port);
// *** GET Routes - display pages ***
// gets the audio stream
app.get('/youtube/:link', async function (req, res) {
console.log("fetching audio of " + req.params.link)
try {
for await (const chunk of stream(req.params.link)) {
res.write(chunk)
}
res.end()
} catch (err) {
console.error(err)
if (!res.headersSent) {
res.writeHead(500)
res.end('internal system error')
}
}
});
app.get('/youtube', async function (req, res) {
var link = req.query.link;
console.log("fetching audio of " + link + " (found by query)")
try {
for await (const chunk of stream(link)) {
res.write(chunk)
}
res.end()
} catch (err) {
console.error(err)
if (!res.headersSent) {
res.writeHead(500)
res.end('internal system error')
}
}
});
// Root Route
app.get('/', function (req, res) {
console.clear();
res.render('index', {
// output: audioFormats
});
});