-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
241 lines (186 loc) · 9.17 KB
/
app.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
'use strict';
//Run packages
const express = require('express');
const bodyParser = require('body-parser');
//Set up app
const app = express();
const port = process.env.PORT || 8090;
app.use(express.static('client'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//Set up entities
let users = [{'fname':'John', 'lname':'Doe','username':'[email protected]', 'bio': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lectus vestibulum mattis ullamcorper velit sed ullamcorper morbi tincidunt.'},
{'fname':'Jane', 'lname':'Smith','username':'[email protected]', 'bio': 'Pellentesque vitae ante vehicula, blandit mauris a, maximus nulla. Donec non enim at velit hendrerit pretium. Nulla iaculis, nunc sit amet volutpat pharetra, erat justo tempor orci, a vulputate tellus turpis sit amet mauris.'},
{'fname':'Tom', 'lname':'Clark','username':'[email protected]', 'bio': 'Integer nibh justo, venenatis at nulla ac, malesuada dignissim dolor. Integer ac vulputate enim. Mauris ac mi a enim consequat maximus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.'}];
let current_user = [];
let my_posts = [];
let posts = [{'posttitle':'Sample Post 1', 'postauthor':'[email protected]','postdate':'2019-04-12', 'postcontent': 'Integer accumsan nunc quis lectus maximus, congue cursus dolor porta. Duis ultrices sapien non elit cursus, vitae vestibulum massa fermentum. Aliquam sed ligula viverra, imperdiet dolor ut, ornare lorem. Phasellus erat neque, viverra non ipsum at, mollis porta mi. Vivamus nulla turpis, rutrum non laoreet ut, molestie sed mi. Suspendisse nec pharetra tellus. Nam vitae nibh non nisi rutrum rhoncus. Aliquam ligula nulla, placerat ut lobortis vel, aliquam eu lectus. Nulla eget suscipit eros. Vivamus dignissim ornare vehicula. Nunc eleifend arcu est, vitae fringilla risus sodales ac. Nulla porta vestibulum elementum. Sed non porttitor massa, id ultrices elit. Donec gravida turpis eget laoreet aliquam.'},
{'posttitle':'Sample Post 2', 'postauthor':'[email protected]','postdate':'2019-04-20', 'postcontent': 'Nulla libero urna, condimentum eget magna sit amet, tempor suscipit enim. Ut consectetur interdum sem, vel pretium risus feugiat eu. Sed in sollicitudin nunc. Aliquam ac scelerisque dui. Quisque consequat congue orci, ac feugiat tellus pulvinar vitae. Maecenas vehicula volutpat diam elementum porttitor. Donec in ornare nunc. Aenean scelerisque est quis mauris vulputate, ut sagittis metus lacinia. Pellentesque ultrices leo ipsum, efficitur ultrices sapien molestie in. Integer mollis porta fermentum. Mauris iaculis risus ac scelerisque viverra.'},
{'posttitle':'Sample Post 3', 'postauthor':'[email protected]','postdate':'2019-04-24', 'postcontent': 'Sed aliquam neque est, vitae consectetur sapien sagittis ac. Sed vitae ornare velit. Aenean ut odio vitae quam tempus aliquet id ultricies nulla. Interdum et malesuada fames ac ante ipsum primis in faucibus. Maecenas tristique, turpis sed tempus aliquet, diam quam vestibulum dolor, nec varius purus sapien et lorem. Sed at euismod erat. Curabitur scelerisque libero quis condimentum tristique. Praesent arcu massa, blandit vel rutrum vitae, porta ac dui. Cras placerat condimentum urna, ut ornare lorem. Suspendisse volutpat eget lorem non pretium. Maecenas varius eu nisi vel auctor. Vestibulum vitae purus nec sem faucibus fringilla. Aliquam lobortis dolor nec hendrerit egestas. Etiam ut tincidunt dolor, nec tincidunt metus.'}];
//Routes
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/client/index.html'));
});
//Collect user data upon registration (user authentication handled by Google Firebase Auth API)
app.post('/newuser', function(req,res){
if(checkUserExists(req.body.username) !== false){
res.send(400);
res.send('A user with this username already exists. Please try again.');
}else if(!req.body.username || !req.body.fname || !req.body.lname || !req.body.password || !req.body.confirmpass){
res.send('Form fields missing. Please complete all fields.');
}else if(req.body.password != req.body.confirmpass){
res.send('Passwords do not match. Please try again.');
}else{
current_user.push(req.body.username);
let user = {
fname: req.body.fname,
lname: req.body.lname,
username: req.body.username,
bio: req.body.bio
};
users.push(user);
res.send(true);
}
});
//Post user profile data upon login
app.post('/login', function(req,res){
if(checkUserExists(req.body.username) !== false){
console.log('This user has already been stored in the list of current users.');
current_user.push(req.body.username);
console.log('CURRENT USER', current_user[0]);
} else if (!req.body.username || !req.body.password ){
res.send(400);
res.send('Form fields missing. Please complete all fields.');
}
else {
current_user.push(req.body.username);
console.log('CURRENT USER', current_user[0]);
let user = {
fname: 'Null',
lname: 'Null',
username: req.body.username,
bio: 'This user was registered and authenticated by Firebase during a previous server session.'
};
users.push(user);
console.log('This user was registered and authenticated by Firebase in a previous session.');
}
});
// Functions for searching among user properties
function checkUserExists(username){
for (var i = 0; i < users.length; i++) {
if (users[i].username == username){
return i;
}
}
return false;
}
function checkFnameUserExists(fname){
for (var i = 0; i < users.length; i++) {
if (users[i].fname == fname){
return i;
}
}
return false;
}
function checkLnameUserExists(lname){
for (var i = 0; i < users.length; i++) {
if (users[i].lname == lname){
return i;
}
}
return false;
}
// List all current users in user directory
app.get('/users/', function(req,res) {
res.send(users);
});
// Create a new forum post
app.post('/createpost',function(req,resp){
if(!req.body.posttitle || !req.body.postdate || !req.body.postcontent ){
resp.send(400);
resp.send('Form fields missing. Please try again.');
}else if (checkPostExists(req.body.posttitle) !== false){
resp.status(400);
resp.send('A post with this title already exists in the system. Please try again.');
}else{
let post= {
posttitle: req.body.posttitle,
postauthor: current_user[0],
postdate: req.body.postdate,
postcontent: req.body.postcontent,
};
posts.push(post);
my_posts.push(post);
}
});
// List all current posts in the post library
app.get('/posts/', function(req,res) {
res.send(posts);
});
// List all of the logged-in user's posts
app.get('/myposts/', function(req,res) {
res.send(my_posts);
});
// Functions for searching among post properties
function checkPostExists(posttitle){
for (var i = 0; i < posts.length; i++) {
if (posts[i].posttitle == posttitle){
return i;
}
}
return false;
}
function checkPostAuthorExists(postauthor){
for (var i = 0; i < posts.length; i++) {
if (posts[i].postauthor == postauthor){
return i;
}
}
return false;
}
// Search in user library by username (user's email address)
app.get('/users/:username', function(req,res){
let index = checkUserExists(req.params.username);
if(index !== false){
res.send(users[index]);
}else{
res.send('No user found with this email address.');
}
});
// Search in user library by first name
app.get('/users/fname/:fname', function(req,res){
let index = checkFnameUserExists(req.params.fname);
if(index !== false){
res.send(users[index]);
}else{
res.send('No user found with this first name.');
}
});
// Search in user library by last name
app.get('/users/lname/:lname', function(req,res){
let index = checkLnameUserExists(req.params.lname);
if(index !== false){
res.send(users[index]);
}else{
res.send('No user found with this last name.');
}
});
// Search in post library by post title
app.get('/posts/:posttitle', function(req,res){
let index = checkPostExists(req.params.posttitle);
if(index !== false){
res.send(posts[index]);
}else{
res.send('No post found with this title.');
}
});
// Search in post library by post author (user's email address)
app.get('/posts/author/:postauthor', function(req,res){
let index = checkPostAuthorExists(req.params.postauthor);
if(index !== false){
res.send(posts[index]);
}else{
res.send('No post found with this author.');
}
});
module.exports = app;