This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
115 lines (93 loc) · 3.58 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
var express = require('express');
var path = require('path');
var logger = require('morgan');
var compression = require('compression');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var expressValidator = require('express-validator');
var dotenv = require('dotenv');
var mongoose = require('mongoose');
var jwt = require('jsonwebtoken');
var moment = require('moment');
var request = require('request');
// Load environment variables from .env file
dotenv.load();
// Models
var User = require('./models/User');
var Committee = require('./models/Committee');
// Controllers
var userController = require('./controllers/user');
var committeeController = require('./controllers/committee');
var contactController = require('./controllers/contact');
var app = express();
mongoose.connect(process.env.MONGODB);
mongoose.connection.on('error', function() {
console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
app.set('port', process.env.PORT || 3000);
app.use(compression());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
req.isAuthenticated = function() {
var token = (req.headers.authorization && req.headers.authorization.split(' ')[1]) || req.cookies.token;
try {
return jwt.verify(token, process.env.TOKEN_SECRET);
} catch (err) {
return false;
}
};
if (req.isAuthenticated()) {
var payload = req.isAuthenticated();
User.findById(payload.sub, function(err, user) {
req.user = user;
next();
});
} else {
next();
}
});
app.post('/contact', contactController.contactPost);
//account
app.put('/account', userController.ensureAuthenticated, userController.accountPut);
app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
app.post('/signup', userController.signupPost);
app.post('/login', userController.loginPost);
app.post('/forgot', userController.forgotPost);
app.post('/reset/:token', userController.resetPost);
app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
app.post('/auth/google', userController.authGoogle);
app.get('/auth/google/callback', userController.authGoogleCallback);
//admin
app.get('/users', userController.ensureAuthenticated, userController.usersGet);
app.put('/user', userController.ensureAuthenticated, userController.updateLevelPut);
app.delete('/user/:id', userController.ensureAuthenticated, userController.userDelete);
app.post('/committees', userController.ensureAuthenticated, committeeController.committeePost);
app.get('/committees', committeeController.committeesGet);
app.put('/committees', userController.ensureAuthenticated, committeeController.committeePut);
app.delete('/committee/:id', committeeController.committeeDelete);
//general
app.get('/committees', committeeController.committeesGet);
app.get('/committees/user/:id', committeeController.committeeUserGet);
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'app', 'index.html'));
});
app.get('*', function(req, res) {
res.redirect('/#' + req.originalUrl);
});
// Production error handler
if (app.get('env') === 'production') {
app.use(function(err, req, res, next) {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;