-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
153 lines (128 loc) · 4.18 KB
/
backend.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const {getAuth} = require('./spotifyAPI.js') ;
/* Setup some stuff */
const http = require('http');
const express = require('express'); // Using Express app for communications
const cors = require('cors'); // CORS
const path = require('path');
// const body_parser = require('body-parser');
const app = express();
app.use(express.json());
// app.use(express.static("express"));
const port = process.env.PORT || 3000;
const server = http.createServer(app);
// app.use(body_parser.json());
app.use(cors());
// Generate random codes
let codes = [{
code: 12345,
songs: ['space jam', '22', 'William cannot authorize']
}];
function randomCode(length) {
var result= '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
app.get('/getCode', function(req,res) {
let code = randomCode(5);
while (codes.reduce((acc, val) => acc || val.code == code), false) {
code = randomCode(5);
}
codes.push({
code: code,
songs: []
});
// console.log('Current Codes: ' + codes);
// console.log('New Code: ' + code);
// res.json({ code: code });
res.send({code});
});
app.get('/removeCode', function(req,res) {
let idx = codes.find(element => element.code == code);
if (idx != -1) {
codes.splice(idx, 1);
}
});
app.post('/addSong', function(req,res) {
console.log('Homie __________________________________________________________________________');
console.log(Object.keys(req));
let code = req.body.code;
let song = req.body.song;
console.log('Code: ' + code);
console.log('Song: ' + song);
console.log('Codes: ' + codes);
let playlist = codes.find(element => element.code == code);
console.log(playlist);
if (playlist != undefined) {
// codes.find(element => element.code == code).songs.push(song);
playlist.songs.unshift(song);
// playlist.songs.push(song);
// res.send('Song added to playlist');
res.json(playlist);
} else {
res.send('Playlist does not exist');
}
});
app.get('/getSongs', function(req,res) {
// res.json({code: 1, songs: ['a', 'b']});
console.log(req);
console.log(Object.keys(req));
console.log("params");
console.log(Object.keys(req.params));
console.log("query");
console.log(Object.keys(req.query));
let info = req.query.code;
let playlist = codes.find(element => element.code == info);
console.log(playlist);
if (playlist != undefined) {
res.json(playlist);
} else {
// What do we do here??
res.send('Bad stuff is happening.');
}
});
//api stuff
app.get('/static/authenticate', async function(req,res) {
//run the api to auth spotify
let test = await getAuth();
res.redirect('https://accounts.spotify.com/authorize' +
'?response_type=code' +
'&client_id=' + test.client_id + '&redirect_uri=' + encodeURIComponent(test.redirect_uri));
// res.send(Object.keys(test.));
// res.end();
// console.log("RIGHT HERE HOMIE --------------------------------------------------------------------------");
// console.log(Object.keys(test.request._redirectable._currentUrl));
// res.send(test);
// res.setHeader("Location", test.request._redirectable._currentUrl);
// res.end();
});
// Serve static files
// http://expressjs.com/en/starter/static-files.html
app.use('/static', express.static('static'));
app.use('/node_modules', express.static('node_modules'));
// Default URL for website
app.use('/', function(req,res) {
res.sendFile(path.join(__dirname+'/static/index.html'));
});
app.use('*', function(req,res) {
res.send("Error, page not found.");
});
/* THESE DON'T WORK ***
// GET
app.get('/whatever', (req, res) => {
console.log(req.query.action);
res.send(`Request received`);
});
// POST
app.post('/', (req, res) => {
console.log(req.query.action);
res.send(`Request received`);
});
*/
// Start listening
server.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});