-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
317 lines (272 loc) · 9.18 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// server.js
// set up ======================================================================
// get all the tools we need
require('dotenv').config();
var express = require('express');
var app = express();
var path = require('path');
var crypto = require('crypto');
var port = process.env.PORT || 8000;
var mongodb = require('mongodb');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var favicon = require('serve-favicon');
var configDB = require('./config/database.js');
var routes = require('./app/routes/index');
var multer = require('multer');
const {GridFsStorage} = require('multer-gridfs-storage');
var _ = require('lodash');
var methodOverride = require('method-override');
//New GridFS Stuff
var Document = require('./app/models/document');
// configuration ===============================================================
const mongoURI = process.env.DB_CONN;
const connection = mongoose.connect(mongoURI);
const conn = mongoose.createConnection(mongoURI); // second connection for gridfs
require('./config/passport');
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
mongoose.connection.on('connected', () => {
console.log('Connected to database!');
});
mongoose.connection.on('error', (err) => {
console.log('Database error: ' + err);
});
// multer file upload settings
let limits = {
files: 1,
fileSize: 1024 * 1024,
fieldSize: 1024 * 1024
};
let fileFilter = function(req, file, cb) {
var allowedMimes = ['application/pdf' ,'application/msword',
'image/jpeg', 'image/png',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template'
];
if (_.includes(allowedMimes, file.mimetype)) {
// allow supported pdf/doc files
cb(null, true);
} else {
// throw error for invalid files
cb(new Error('Only pdf/doc/docx filetypes are supported.'));
}
};
// once gridfs database is connected
let gfs, storage, upload;
conn.once('open', function() {
gfs = new mongodb.GridFSBucket(conn.db);
gfs.on('error', (err) => {
console.log('GridFS database error: ' + err);
});
});
conn.on('dberror', (err) => {
console.log('GridFS database error: ' + err);
});
storage = new GridFsStorage({
db: conn,
file: (req, file) => {}
});
upload = multer ({
storage: storage,
limits: limits,
fileFilter: fileFilter
});
app.use(express.static(path.join(__dirname, 'public')));
// Set favicon to the SST crest
app.use(favicon(__dirname + '/public/images/logo.png'));
app.set('view engine', 'ejs'); // set up ejs for templating
// required for passport
app.use(session({ secret: process.env.SECRET_KEY, name: process.env.SESSION_NAME })); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
app.post('/uploadItecResume', upload.single('itecResume'), (req, res) => {
// res.json({ file: req.file.grid });
console.log(req.file.grid);
//Test document object to see if you can add a document from inside
//this function. It works.
var document = new Document({
'user' : {
'user_id' : req.user.studentid,
'fname': req.user.fname,
'lname': req.user.lname,
'user_email': req.user.email
},
'documentName' : req.file.originalname,
'fileId' : req.file.filename,
'fileType' : 'Resume',
'fileSection' : 'ITEC',
'documentStatus' : 'submitted',
});
document.save(function(err) {
if (err) {
throw err;
} else {
console.log('document added!!');
}
});
res.redirect('/home');
});
app.post('/uploadItecFerpa', upload.single('itecFerpa'), (req, res) => {
// res.json({ file: req.file.grid });
console.log(req.file.grid);
//Test document object to see if you can add a document from inside
//this function. It works.
var document = new Document({
'user' : {
'user_id' : req.user.studentid,
'fname': req.user.fname,
'lname': req.user.lname,
'user_email': req.user.email
},
'documentName' : req.file.originalname,
'fileId' : req.file.filename,
'fileType' : 'Ferpa',
'fileSection' : 'ITEC',
'documentStatus' : 'submitted',
});
document.save(function(err) {
if (err) {
throw err;
} else {
console.log('document added!!');
}
});
res.redirect('/home');
});
app.post('/uploadItecOther', upload.single('itecOther'), (req, res) => {
// res.json({ file: req.file.grid });
console.log(req.file.grid);
//Test document object to see if you can add a document from inside
//this function. It works.
var document = new Document({
'user' : {
'user_id' : req.user.studentid,
'fname': req.user.fname,
'lname': req.user.lname,
'user_email': req.user.email
},
'documentName' : req.file.originalname,
'fileId' : req.file.filename,
'fileType' : req.body.itecOther,
'fileSection' : 'ITEC',
'documentStatus' : 'submitted',
});
document.save(function(err) {
if (err) {
throw err;
} else {
console.log('document added!!');
}
});
res.redirect('/home');
});
app.post('/uploadBioEssay', upload.single('bioEssay'), (req, res) => {
// res.json({ file: req.file.grid });
console.log(req.file.grid);
//Test document object to see if you can add a document from inside
//this function. It works.
var document = new Document({
'user' : {
'user_id' : req.user.studentid,
'fname': req.user.fname,
'lname': req.user.lname,
'user_email': req.user.email
},
'documentName' : req.file.originalname,
'fileId' : req.file.filename,
'fileType' : 'Essay',
'fileSection' : 'BIO',
'documentStatus' : 'submitted',
});
document.save(function(err) {
if (err) {
throw err;
} else {
console.log('document added!!');
}
});
res.redirect('/home');
});
app.post('/uploadBioTranscript', upload.single('bioTranscript'), (req, res) => {
// res.json({ file: req.file.grid });
console.log(req.file.grid);
//Test document object to see if you can add a document from inside
//this function. It works.
var document = new Document({
'user' : {
'user_id' : req.user.studentid,
'fname': req.user.fname,
'lname': req.user.lname,
'user_email': req.user.email
},
'documentName' : req.file.originalname,
'fileId' : req.file.filename,
'fileType' : 'Transcript',
'fileSection' : 'BIO',
'documentStatus' : 'submitted',
});
document.save(function(err) {
if (err) {
throw err;
} else {
console.log('document added!!');
}
});
res.redirect('/home');
});
app.post('/uploadBioOther', upload.single('bioOther'), (req, res) => {
// res.json({ file: req.file.grid });
console.log(req.file.grid);
//Test document object to see if you can add a document from inside
//this function. It works.
var document = new Document({
'user' : {
'user_id' : req.user.studentid,
'fname': req.user.fname,
'lname': req.user.lname,
'user_email': req.user.email
},
'documentName' : req.file.originalname,
'fileId' : req.file.filename,
'fileType' : req.body.bioOther,
'fileSection' : 'BIO',
'documentStatus' : 'submitted',
});
document.save(function(err) {
if (err) {
throw err;
} else {
console.log('document added!!');
}
});
res.redirect('/home');
});
app.get('/getFiles/:filename/:documentName', (req, res) => {
console.log("Doc name:" + req.params.documentName);
// first check if file exists
const cursor = gfs.find({filename : req.params.filename});
gfs.openDownloadStreamByName(req.params.filename)
.on('error', (err) => {
return res.status(404).json({
msg: 'Could not find file with id: ' + req.params.filename +
' and with name: ' + req.params.documentName,
err: err
});
})
.pipe(res);
});
require('./app/routes/index.js')(app, passport);
// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);