-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
232 lines (192 loc) · 6.04 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Dependencies
var express = require("express");
var mongojs = require("mongojs");
var logger = require("morgan");
var bodyParser = require('body-parser');
var Spotify = require("node-spotify-api");
var request = require('request');
var path = require('path');
var spotify = new Spotify({
id: process.env.SPOTIFY_ID,
secret: process.env.SPOTIFY_SECRET
});
var PORT = process.env.PORT || 3001;
var app = express();
// Set the app up with morgan
app.use(logger("dev"));
app.use(bodyParser());
// Database configuration
var databaseUrl = process.env.MONGODB_URI || "songs_db";
var collections = ["songs"];
// Hook mongojs config to db variable
var db = mongojs(databaseUrl , collections);
// Log any mongojs errors to console
db.on("error", function(error) {
console.log("Database Error:", error);
});
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
/*
if we don't do this here then we'll get this error in apps that use this api
Fetch API cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
read up on CORs here: https://www.maxcdn.com/one/visual-glossary/cors/
*/
//allow the api to be accessed by other apps
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
next();
});
// movie Routes
app.get("/movies/:movie", function(req, res) {
var url = "http://www.omdbapi.com/?t=" + req.params.movie;
request(url, function (error, response, body) {
res.json(JSON.parse(response.body));
});
});
// songs Routes
// ======
//documentation for mongojs:
//https://github.com/mafintosh/mongojs
app.get("/songs", function(req, res) {
//sort songs
db.songs.aggregate(
[
{ $sort : { votes : -1 } }
], function(error, songs){
res.json(songs);
});
// Find all songs in the songs collection
// db.songs.find({}, function(error, songs) {
// // Log any errors
// if (error) {
// console.log(error);
// }
// // Otherwise, send json of the songs back to user
// // This will fire off the success function of the ajax request
// else {
// //pretend that it takes 5 seconds to get the songs back
// //setTimeout(function(){
// res.json(songs);
// //}, 5000)
// }
// });
});
// Handle form submission, save submission to mongo
app.post("/songs", function(req, res) {
// Insert the song into the songs collection
db.songs.insert(req.body, function(error, savedSong) {
// Log any errors
if (error) {
console.log(error);
}else {
//the reason why we are sending the savedSong back is because we now have an _id to give to the client
res.json(savedSong);
}
});
});
app.get("/songs/:artist/:songname", function(req, res) {
var query = req.params.songname;
spotify.search({ type: "track", query: query}, function(err, data) {
if (err) res.json(err);
// console.log(data)
var songs = data.tracks.items;
var data = [];
// Helper function that gets the artist name
var getArtistNames = function(artist) {
return artist.name;
};
for (var i = 0; i < songs.length; i++) {
data.push({
"artist": songs[i].artists.map(getArtistNames),
"songName": songs[i].name,
"previewSong": songs[i].preview_url,
"album": songs[i].album.name
});
}
res.json(data);
});
})
//one song
app.get("/songs/:id", function(req, res) {
db.songs.findOne({
"_id": mongojs.ObjectId(req.params.id)
}, function(error, oneSong) {
if (error) {
res.send(error);
}else {
res.json(oneSong);
}
});
});
//update a song
app.put("/songs/:id", function(req, res) {
//if we use this then we won't get the updated document back
/*
db.songs.update({
"_id": mongojs.ObjectId(req.params.id)
}, {
$set: {
"artist": req.body.artist,
"songName": req.body.songName
}
}, function(error, editedSong) {
if (error) {
res.send(error);
}else {
res.json(editedSong);
}
});
*/
db.songs.findAndModify({
query: {
"_id": mongojs.ObjectId(req.params.id)
},
update: { $set: {
"artist": req.body.artist, "songName": req.body.songName }
},
new: true
}, function (err, editedSong) {
res.json(editedSong);
});
});
// /songs/votes/j483u843384/up
// /songs/votes/j483u843384/down
// /songs/votes/j483u843384/alabama
app.put("/songs/votes/:id/:direction", function(req, res){
var voteChange = 0;
if (req.params.direction == 'up') voteChange = 1;
else voteChange = -1;
//this is wrong I want to grab the current votes and increment by 1
db.songs.findAndModify({
query: {
"_id": mongojs.ObjectId(req.params.id)
},
update: { $inc: { votes: voteChange} },
new: true
}, function (err, editedSong) {
res.json(editedSong);
});
});
app.delete("/songs/:id", function(req, res) {
var id = req.params.id;
db.songs.remove({
"_id": mongojs.ObjectID(id)
}, function(error, removed) {
if (error) {
res.send(error);
}else {
res.json(id);
}
});
});
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, './client/public/index.html'));
});
// Listen on port 3001
app.listen(PORT, function() {
console.log('🌎 ==> Now listening on PORT %s! Visit http://localhost:%s in your browser!', PORT, PORT);
});