-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (49 loc) · 1.4 KB
/
server.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
const express = require('express');
const app = express();
const port = 5003;
const fs = require('fs');
const path = require('path');
const ytdl = require('ytdl-core');
//use cors to allow cross origin resource sharing
// app.use(
// cors({
// origin: 'http://localhost:3000',
// credentials: true,
// })
// );
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get('/api', (req, res) => {
console.log('it works!');
});
var urls;
function datas(){
app.post('/data', (req, res) => {
urls = req.body.link;
return res.json({
"message": "filled!"
})
});
return urls;
};
datas();
app.get('/downloads', (req, res) => {
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
var fileName = makeid(8) + ".mp3"
res.header('Content-Disposition', 'attachment; filename=" '+ fileName +'"');
var url3 = datas();
ytdl(url3, {
quality: 'highestaudio',
filter: 'audioonly'
}).pipe(res)
})
app.listen(port, () => console.log(`Server started on port ${port}`));