-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
261 lines (219 loc) · 5.74 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const express = require('express')
const path = require('path');
const mongo = require('mongodb');
const createError = require('http-errors');
const cookieParser = require('cookie-parser');
const monk = require('monk');
const fs = require('fs');
const mongoose = require('mongoose');
const User = require('./models/User');
const logger = require('morgan');
const cors = require('cors');
const chalk = require('chalk');
const bodyParser = require('body-parser');
require('dotenv').config()
const app = express();
// Routes
const userRoutes = require('./routes/User')
const url = process.env.MONGOLAB_URI;
// Database Setup
mongoose.Promise = global.Promise;
mongoose
.connect(url, { useNewUrlParser: true })
.then(() => console.log(chalk.green('Connected to DB')))
.catch(err => console.log(chalk.red(`Error connecting to DB. Error: ${err}`)));
// Middleware
app.use(logger('dev'))
app.use(cors())
app.use(bodyParser.json())
app.use('/api', userRoutes)
// Start Server
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(chalk.green(`Server running on Port:${PORT}`))
})
var db = mongoose.connection;
//handle mongo error
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
});
// access databse
app.use(function(req,res,next){
req.db = db;
next();
});
// gets all images for gallery display
const imagePath = path.join(path.resolve(__dirname, 'client/src/assets'), '/images');
app.use(express.static(imagePath))
function getDirectoryContent(req, res, next) {
fs.readdir(imagePath , function (err, images) {
if (err) { return next(err); }
res.locals.file = images;
next();
});
}
app.get('/api/images', getDirectoryContent, function(req, res) {
// tests for jpg then sends files
const regexp = /.jpg/;
const images = [];
res.locals.file.forEach(file => {
if (regexp.test(file)) {
images.push(file);
}
});
res.send(images);
});
// find categories
app.post('/api/categories', function (req, res) {
User.findOne({ _id: req.body.userId }, { categories: 1 }
, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
// And forward to success page
res.json(doc);
}
});
})
// return all user images
app.post('/api/user-gallery', function (req, res) {
User.findOne({ _id: req.body.userId }, { images: 1}
, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
// And forward to success page
res.json(doc);
}
});
})
// find user gallery by categories
app.post('/api/user-category', function (req, res) {
User.findOne(
{ _id: req.body.userId }
, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
// And forward to success page
res.json(doc);
}
});
})
// remove a category
app.post('/api/remove-category', function (req, res) {
User.update({ _id: req.body.userId }, {
$pull: {
categories: req.body.category,
}
}, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
res.json(req.body.category)
}
});
})
// add a category
app.post('/api/add-category', function (req, res) {
User.update({ _id: req.body.userId }, {
$addToSet: {
categories: req.body.category,
}
}, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
res.json(req.body.category)
}
});
})
// add a user input image url
app.post('/api/add-user-image', function (req, res) {
const image = {
fileName: req.body.image,
category: req.body.category,
}
User.update({ _id: req.body.userId }, {
$addToSet: {
images: image,
}
}, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
// And forward to success page
res.json(req.body.image)
}
});
})
// add image object
app.post('/api/add-image-to-account', function (req, res) {
const image = {
fileName: req.body.image,
category: req.body.category,
userSubmitted: req.body.userSubmitted,
}
User.update({ _id: req.body.userId }, {
$addToSet: {
images: image,
}
}, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
// And forward to success page
res.json(image)
}
});
})
app.post('/api/remove-image-from-account', function (req, res) {
const image = req.body.image
User.update({ _id: req.body.userId }, {
$pull: { images : { fileName : image }, }
}, function (err, doc) {
if (err) {
// If it failed, return error
res.json(err);
}
else {
// And forward to success page
res.json(doc)
}
});
})
app.post('/api/update-first-login', (req, res) => {
User.update({ _id: req.body.userId }, {
$set: { 'onboarding.firstLogin': false },
}, function (err, doc) {
if (err) {
res.json(err)
}
else {
res.json(doc)
}
});
})
app.use(express.static(path.join(__dirname, './client/build')));
app.get('/*', function(req, res) {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
})