-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
190 lines (149 loc) · 6.28 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// server.js
// where your node app starts
// we've started you off with Express (https://expressjs.com/) and axios (https://www.npmjs.com/package/axios)
// but feel free to use whatever libraries or frameworks you'd like through `package.json`.
const express = require("express");
const axios = require("axios");
const { getAccessToken } = require("./spotify/auth");
const { searchTracks, searchArtist, getRecommendations } = require("./spotify/actions");
const BASE_URL = "https://api.spotify.com/v1"
// initialize an express instance called 'app'
const app = express();
// Log an error message if any of the secret values needed for this app are missing
if (!process.env.SPOTIFY_CLIENT_ID || !process.env.SPOTIFY_CLIENT_SECRET) {
console.error("ERROR: Missing one or more critical Spotify environment variables. Check .env file");
}
// set up the app to parse JSON request bodies
app.use(express.json());
// make all the files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));
// return the index.html file when a GET request is made to the root path "/"
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
// app.post("/recommendations", async (req, res) => {
// if(!req.body) {
// return res.status(400).send({ message: "Bad Request - must send a JSON body with track and artist" })
// }
// const { track, artist } = req.body
// if(!track || !artist) {
// return res.status(400).send({ message: "Bad Request - must pass a track and artist" })
// }
// // 1. Get access token
// let accessToken
// try {
// accessToken = await getAccessToken()
// } catch(err) {
// console.error(err.message)
// return res.status(500).send({ message: "Something went wrong when fetching access token" })
// }
// // Create an instance of axios to apply access token to all request headers
// const http = axios.create({ headers: { 'Authorization': `Bearer ${accessToken}` }})
// // 2. get track id from search
// let trackId;
// try {
// const result = await searchTracks(http, { track, artist })
// const { tracks } = result
// if(!tracks || !tracks.items || !tracks.items.length ) {
// return res.status(404).send({ message: `Song '${track}' by ${artist} not found.` })
// }
// // save the first search result's trackId to a variable
// trackId = tracks.items[0].id
// } catch(err) {
// console.error(err.message)
// return res.status(500).send({ message: "Error when searching tracks" })
// }
// // 3. get song recommendations
// try {
// const result = await getRecommendations(http, { trackId })
// const { tracks } = result
// // if no songs returned in search, send a 404 response
// if(!tracks || !tracks.length ) {
// return res.status(404).send({ message: "No recommendations found." })
// }
// // Success! Send track recommendations back to client
// return res.send({ tracks })
// } catch(err) {
// console.error(err.message)
// return res.status(500).send({ message: "Something went wrong when fetching recommendations" })
// }
// });
app.post("/recommendations", async (req, res) => {
if(!req.body) {
return res.status(400).send({ message: "Bad Request - must send a JSON body with track and artist" })
}
const { artist1, artist2, artist3 } = req.body;
if(!artist1 || !artist2 || !artist3) {
return res.status(400).send({ message: "Bad Request - must pass names of three artists" })
}
// 1. Get access token
let accessToken
try {
accessToken = await getAccessToken();
} catch(err) {
console.error(err.message);
return res.status(500).send({ message: "Something went wrong when fetching access token" });
}
// Create an instance of axios to apply access token to all request headers
const http = axios.create({ headers: { 'Authorization': `Bearer ${accessToken}` }})
// 2. get artist id from search
let artistId1;
let artistId2;
let artistId3;
try {
const result1 = await searchArtist(http, artist1);
const artists1 = result1;
if(!artists1.artists || !artists1.artists.items || !artists1.artists.items.length) {
return res.status(404).send({ message: `${artist1} not found.` })
}
// save the first search result's artistId to a variable
artistId1 = artists1.artists.items[0].id
} catch(err) {
console.error(err.message)
return res.status(500).send({ message: "Error when searching tracks" })
}
try {
const result2 = await searchArtist(http, artist2);
const artists2 = result2;
if(!artists2.artists || !artists2.artists.items || !artists2.artists.items.length) {
return res.status(404).send({ message: `${artist2} not found.` })
}
// save the first search result's artistId to a variable
artistId2 = artists2.artists.items[0].id
} catch(err) {
console.error(err.message)
return res.status(500).send({ message: "Error when searching tracks" })
}
try {
const result3 = await searchArtist(http, artist3);
const artists3 = result3;
if(!artists3.artists || !artists3.artists.items || !artists3.artists.items.length) {
return res.status(404).send({ message: `${artist3} not found.` })
}
// save the first search result's artistId to a variable
artistId3 = artists3.artists.items[0].id
} catch(err) {
console.error(err.message)
return res.status(500).send({ message: "Error when searching tracks" })
}
// 3. get song recommendations
try {
const result = await getRecommendations(http, { artistId1, artistId2, artistId3 })
const { tracks } = result
console.log(tracks);
// if no songs returned in search, send a 404 response
if(!tracks || !tracks.length ) {
return res.status(404).send({ message: "No recommendations found." })
}
// Success! Send track recommendations back to client
return res.send({ tracks })
} catch(err) {
console.error(err.message)
return res.status(500).send({ message: "Something went wrong when fetching recommendations" })
}
});
// after our app has been set up above, start listening on a port provided by Glitch
app.listen(process.env.PORT, () => {
console.log(`Example app listening at port ${process.env.PORT}`);
});