-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·260 lines (236 loc) · 7.03 KB
/
index.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const express = require('express');
const http = require('http');
const sslRedirect = require('heroku-ssl-redirect');
const compression = require('compression');
const path = require('path');
const MongoClient = require('mongodb').MongoClient;
const app = express();
const url = require('url');
const bodyParser = require('body-parser');
const UIDGenerator = require('uid-generator');
const uidgen = new UIDGenerator(256, UIDGenerator.BASE62);
const mourl = String(process.argv[2]);
console.log(mourl);
app.use(sslRedirect());
app.use(compression(({ level: 9 })));
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
/**
* Test DB Connection
*/
MongoClient.connect(mourl, function (err, db) {
if (err) throw err;
console.log("Database usable!");
});
/**
* Get User Details Endpoint
*/
app.get('/loginUser', function (request, response) {
var querys = url.parse(request.url, true);
var email = querys.query.email;
var pass = querys.query.pass;
MongoClient.connect(mourl, function (err, db) {
var search = JSON.parse(`{"email": "${email}", "password": "${pass}"}`);
db.collection("users").find(search).toArray(function (err, result) {
if (err) {
console.error(err); response.send("Error " + err); throw err;
}
else {
response.send(result);
}
});
});
});
/**
* Check Existing User Endpoint
*/
app.get('/getUserDetails', function (request, response) {
var querys = url.parse(request.url, true);
var email = querys.query.email;
MongoClient.connect(mourl, function (err, db) {
var search = JSON.parse(`{"email": "${email}"}`);
db.collection("users").find(search).toArray(function (err, result) {
if (err) {
console.error(err); response.send("Error " + err); throw err;
}
else {
response.send(result);
}
});
});
});
/**
* Register User Endpoint
*/
app.get('/regUser', function (request, response) {
var querys = url.parse(request.url, true);
var name = querys.query.name;
var email = querys.query.email;
var pass = querys.query.password1;
var tracking_data = {};
var uid = uidgen.generateSync();
console.log(uid);
var seedData = {
"uname": `${name}`,
"email": `${email}`,
"password": `${pass}`,
"token": uid,
"reactions_data": tracking_data,
"bookmark_data": tracking_data,
"clicked_data": tracking_data,
"cluster": ``,
"genre_ranking": ``
};
MongoClient.connect(mourl, function (err, db) {
var search = JSON.parse(`{"email": "${email}"}`);
console.log(search);
db.collection("users").find(search).toArray(function (err, result) {
if (err) { console.error(err); response.send("Error " + err); throw err; }
if (result.length == 0) {
console.log("NO user")
db.collection("users").insertOne(seedData, function (err, res) {
if (err) throw err;
console.log("1 user inserted");
response.send(res);
});
}
else { response.send(result); }
db.close();
});
});
});
/**
* Reaction data Endpoint
*/
app.post('/sendReactionData', function (req, res) {
var user_id = req.body.user_id;
var tracking_data = {};
tracking_data = JSON.parse(req.body.JSON_String);
var bcd = tracking_data;
var search = JSON.parse(`{"email": "${user_id}"}`);
var query_object, abc, query, params;
try {
MongoClient.connect(mourl, function (err, db) {
for (var movie_id in tracking_data) {
kcd = JSON.stringify(bcd[movie_id]);
abc = "\"reactions_data." + movie_id + "\"";
if (params === undefined) {
params = abc + ': ' + kcd;
}
else {
params = params + ',' + abc + ': ' + kcd;
}
query = '{$set: {' + params + '}}';
var jsonValidString = JSON.stringify(eval("(" + query + ")"));
var query_object = JSON.parse(jsonValidString);
}
// Send to DB
db.collection("users").update(search, query_object, { upsert: true }, function (err, result) {
if (err) {
console.error(err);
res.send("Error " + err); throw err;
}
else {
res.send(result);
}
});
});
}
catch (e) {
console.log(e);
}
});
/**
* Clicks Data Endpoint
*/
app.post('/sendClicksData', function (req, res) {
var user_id = req.body.user_id;
var additional_data = {};
additional_data = JSON.parse(req.body.JSON_String);
var bcd = additional_data;
var search = JSON.parse(`{"email": "${user_id}"}`);
var query_object, abc, query, params;
try {
MongoClient.connect(mourl, function (err, db) {
for (var movie_id in additional_data) {
kcd = JSON.stringify(bcd[movie_id]);
abc = "\"clicked_data." + movie_id + "\"";
if (params === undefined) {
params = abc + ': ' + kcd;
}
else {
params = params + ',' + abc + ': ' + kcd;
}
query = '{$set: {' + params + '}}';
var jsonValidString = JSON.stringify(eval("(" + query + ")"));
var query_object = JSON.parse(jsonValidString);
}
db.collection("users").update(search, query_object, { upsert: true }, function (err, result) {
if (err) {
console.error(err);
res.send("Error " + err); throw err;
}
else {
res.send(result);
}
});
});
}
catch (e) {
console.log(e);
}
});
/**
* Bookmark Data Endpoint
*/
app.post('/sendBookmarkData', function (req, res) {
var user_id = req.body.user_id;
var additional_data = {};
additional_data = JSON.parse(req.body.JSON_String);
var bcd = additional_data;
var search = JSON.parse(`{"email": "${user_id}"}`);
var query_object, abc, query, params;
try {
MongoClient.connect(mourl, function (err, db) {
for (var movie_id in additional_data) {
kcd = JSON.stringify(bcd[movie_id]);
abc = "\"bookmark_data." + movie_id + "\"";
if (params === undefined) {
params = abc + ': ' + kcd;
}
else {
params = params + ',' + abc + ': ' + kcd;
}
query = '{$set: {' + params + '}}';
var jsonValidString = JSON.stringify(eval("(" + query + ")"));
var query_object = JSON.parse(jsonValidString);
}
// Send to DB
db.collection("users").update(search, query_object, { upsert: true }, function (err, result) {
if (err) {
console.error(err);
res.send("Error " + err); throw err;
}
else {
res.send(result);
}
});
});
}
catch (e) {
console.log(e);
}
});
/**
* For using Angular generated templates with Node.js
* Keep it in the end of the requests
*/
app.use(express.static(path.join(__dirname, 'dist/'), { maxAge: '5d' }));
app.get('*', function (req, res) {
const index = path.join(__dirname, 'dist/', 'index.html');
res.sendFile(index);
});
const port = process.env.PORT || '3001';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`Running`));