-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
243 lines (177 loc) · 6.41 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
233
234
235
236
237
238
239
240
241
242
243
//Express declaration
var express = require('express');
var app = express();
//Mongo declaration
var mongojs = require('mongojs');
var db = mongojs('MusicallyMe', ['accountCollection']);
var dbm = mongojs('MusicallyMe', ['musicCollection']);
/////////////////////Global Variables (for Mongo)/////////
var mongoData;
///////////////////////////////////////////////////////////
//////////////MongoDB functions///////////////////////////
function registerAccount(username, inputPassword){
//uses find to check if username currently exists
db.accountCollection.find({account: username}).toArray(function(err,result){
if(err){
//database error
console.log("error in registerAccount");
}
else if (result.length){
//Username already exists
console.log("Username already exists!");
}
else{
//here we can insert the account, since there was no error and username does not already exist
db.accountCollection.insert({
account: username,
password: inputPassword,
picture: ""
});
console.log(username + " Registered");
}
});
}
var songs = [];
function addMusic(data){
songs.push([data.songs.song1]);
songs.push([data.songs.song2]);
songs.push([data.songs.song3]);
}
function addMusic2(data){
dbm.musicCollection.insert({
account:data.user,
song1:data.songs.song1,
song2:data.songs.song2,
song3:data.songs.song3,
image1:data.images.img1,
image2:data.images.img2,
image3:data.images.img3,
image4:data.images.img4,
image5:data.images.img5,
});
var songs = [];
var roop = dbm.musicCollection.find({}).toArray(function(err,result){
console.log(result[0]);
});
}
// required to support parsing of POST request bodies
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// put all of your static files (e.g., HTML, CSS, JS, JPG) in the static_files/
app.use(express.static('static_files'));
//required to support parsing of cookies
var cookieParser = require('cookie-parser');
app.use(cookieParser());
app.get('/home', function(req,res){
console.log("Cookies: ", req.cookies['account']);
var nameToLookup = req.cookies['account']; // this matches the '*' part of '/users/*' above
db.accountCollection.find({account: nameToLookup}).toArray(function(err,result){
if(err){
//database error
console.log("error in findAccount");
}
else if (result.length){ //if found
console.log("Username found" + result[0]["account"]);
// res.send(result);
res.cookie('account' , nameToLookup).send(result);
return;
}
else{ //not found
console.log("Username not found!");
res.json();
}
});
});
// CREATE a new user
//
// To test with curl, run:
// curl -X POST --data "name=Carol&job=scientist&pet=dog.jpg" http://localhost:3000/users
app.post('/users', function (req, res) {
//postBody is body of POST req
var postBody = req.body;
var username = postBody.username; //username and password here, both plaintext
var password = postBody.password;
// must have a name!
if (!username) {
res.send('No Username');
return; // return early!
}
registerAccount(username, password);
res.send('OK');
});
// READ profile data for a user
//
// To test with curl, run:
// curl -X GET http://localhost:3000/users/Philip
// curl -X GET http://localhost:3000/users/Jane
app.get('/users/*', function (req, res) {
var nameToLookup = req.params[0]; // this matches the '*' part of '/users/*' above
db.accountCollection.find({account: nameToLookup}).toArray(function(err,result){
if(err){
//database error
console.log("error in findAccount");
}
else if (result.length){ //if found
console.log("Username found" + result[0]["account"]);
// res.send(result);
res.cookie('account' , nameToLookup).send(result);
return;
}
else{ //not found
console.log("Username not found!")
}
});
// res.send('{}'); // failed, so return an empty JSON object!
});
// UPDATE a user's profile with the data given in POST
//
// To test with curl, run:
// curl -X PUT --data "job=bear_wrangler&pet=bear.jpg" http://localhost:3000/users/Philip
app.put('/users/*', function (req, res) {
var nameToLookup = req.params[0]; // this matches the '*' part of '/users/*' above
// try to look up in fakeDatabase
postBody = req.body;
// for (key in postBody) {
// var value = postBody[key];
// // e[key] = value;
// db.accountCollection.update({account: nameToLookup},{
// key:value
// });
// }
// db.accountCollection.update({account: nameToLookup},{ $set: { picture: postBody["picture"] } });
db.accountCollection.update({account: nameToLookup},{ $set: { picture: postBody["picture"] } });
res.send('OK');
return; // return early!
res.send('ERROR'); // nobody in the database matches nameToLookup
});
// DELETE a user
//
// To test with curl, run:
// curl -X DELETE http://localhost:3000/users/Philip
// curl -X DELETE http://localhost:3000/users/Jane
app.delete('/users/*', function (req, res) {
var nameToLookup = req.params[0]; // this matches the '*' part of '/users/*' above
// try to look up in fakeDatabase
console.log(nameToLookup);
//remove function to delete the account
db.accountCollection.remove({account:nameToLookup});
res.send('OK'); // nobody in the database matches nameToLookup
});
// start the server on http://localhost:3000/
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Server started at http://localhost:%s/', port);
});
////////////////////////////MUSIC NOW//////////////////////////////////////
app.post('/music/', function(req, res) {
var data = req.body;
addMusic(data);
var s1 = Math.floor((Math.random() * songs.length));
var s2 = Math.floor((Math.random() * songs.length));
var s3 = Math.floor((Math.random() * songs.length));
var ss1 = songs[s1];
var ss2 = songs[s2];
var ss3 = songs[s3];
res.send(ss1+", "+ss2+", "+ss3);
});