-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
154 lines (136 loc) · 4.5 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
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
154
//1.
var http = require('http');
var fs = require('fs');
const request = require('request');
var url = require('url');
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 555 });
var accessToken = "";
var refreshToken = "";
var tokenReceived = false;
var tokenRequested = false;
wss.on('connection', function connection(wsi) {
wsi.on('message', function incoming(data) {
wss.clients.forEach(function each(client) {
if(client === wsi && data === "token" && tokenReceived) {
var ans = JSON.stringify({
access:accessToken,
refresh:refreshToken
});
client.send(ans);
}
});
});
});
const ws = new WebSocket("ws://localhost:555");
var cred;
fs.readFile("credentials.json", function(error, content) {
cred = JSON.parse(content);
return 0;
});
//2.
var server = http.createServer(function (req, resp) {
//3.
var url_parts = new url.URL("http://localhost" + req.url);
if (url_parts.pathname === "/spotify") {
fs.readFile("index.html", function (error, pgResp) {
if (error) {
resp.writeHead(404);
resp.write('Contents you are looking are Not Found');
return resp.end();
} else {
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write(pgResp);
return resp.end();
}
}
);
}
else if (url_parts.pathname === "/spotify/connect") {
fs.readFile("credentials.json", function(error, content) {
cred = JSON.parse(content);
if (error) {
console.log("error reading credentials");
} else {
resp.writeHead(302, {
"Location": "https://accounts.spotify.com/authorize/?client_id=" + cred.client_id
+ "&redirect_uri=" + "http://localhost/spotify/callback" + "&response_type=code"
+ "&scope=" + encodeURIComponent("streaming user-read-email user-read-private")
//add other headers here...
});
resp.end();
}
});
}
else if (url_parts.pathname === "/spotify/callback") {
var code = url_parts.searchParams.get('code');
var query = new url.URL('https://accounts.spotify.com/api/token');
var body_for_ws = "";
query.searchParams.append('code', code);
query.searchParams.append('client_id', cred.client_id);
query.searchParams.append('client_secret', cred.client_secret);
query.searchParams.append('redirect_uri', "http://localhost/spotify/callback");
query.searchParams.append('grant_type', "authorization_code");
request.post({url: "https://accounts.spotify.com/api/token",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
form : {
code:code,
client_id:cred.client_id,
client_secret:cred.client_secret,
redirect_uri:"http://localhost/spotify/callback",
grant_type:"authorization_code"
}
},
(error, res, body) => {
if (error) {
console.error(error);
return;
}
resp.writeHead(302, {
"Location": "http://localhost/spotify"
//add other headers here...
});
resp.end();
accessToken = JSON.parse(body).access_token;
refreshToken = JSON.parse(body).refresh_token;
tokenReceived = true;
return 0;
})
return 0;
}
else if(req.url.indexOf('.js') != -1) {
fs.readFile("." + req.url, function (error, data) {
if (error) {
resp.writeHead(404, {"Content-type":"text/plain"});
resp.end("No Javascript Page Found.");
} else{
resp.writeHead(200, {'Content-Type': 'text/javascript'});
resp.write(data);
resp.end();
}
});
}
else if(req.url.indexOf('.css') != -1) {
fs.readFile("." + req.url, function (error, data) {
if (error) {
resp.writeHead(404, {"Content-type":"text/plain"});
resp.end("No css Page Found.");
} else{
resp.writeHead(200, {'Content-Type': 'text/css'});
resp.write(data);
resp.end();
}
});
}
else {
//4.
resp.writeHead(200, { 'Content-Type': 'text/html' });
resp.write("URL unknown");
resp.end();
}
});
//5.
server.listen(80);
console.log('Server Started listening on 80');