-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
136 lines (117 loc) · 4.17 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
const express = require('express');
const path = require('path');
const logger = require('morgan');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const dotenv = require('dotenv');
const React = require('react');
const ReactDOM = require('react-dom/server');
const Router = require('react-router');
const Provider = require('react-redux').Provider;
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const moment = require('moment');
const request = require('request');
// Load environment constiables from .env file
dotenv.load();
// ES6 Transpiler
require('babel-core/register');
require('babel-polyfill');
// Models
const User = require('./models/User');
// Controllers
const userController = require('./controllers/user');
const settingsController = require('./controllers/settings');
// React and Server-Side Rendering
const routes = require('./app/routes');
const configureStore = require('./app/store/configureStore').default;
const app = express();
mongoose.connect(process.env.MONGODB || process.env.MONGODB_URI || "mongodb://localhost:27017/tally");
mongoose.connection.on('error', () => {
console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
// Database Init
const startConfig = require('./config/start');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
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((req, res, next) => {
req.isAuthenticated = () => {
const 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()) {
const payload = req.isAuthenticated();
User.findById(payload.sub, (err, user) => {
req.user = user;
next();
});
} else {
next();
}
});
// API
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('/checkin', userController.checkin);
app.post('/reset/:token', userController.resetPost);
app.get('/api/users', userController.getAllUsers);
app.get('/api/users/:slackUsername', userController.getUserBySlack);
app.put('/api/settings', settingsController.settingsPut);
app.get('/api/settings', settingsController.settingsGet);
app.post('/api/settings', settingsController.settingsPost);
app.delete('/api/settings', settingsController.settingsDelete);
// React server rendering
app.use((req, res) => {
const initialState = {
auth: { token: req.cookies.token, user: req.user },
admin: { userList: [] },
messages: {}
};
const store = configureStore(initialState);
Router.match({ routes: routes.default(store), location: req.url }, (err, redirectLocation, renderProps) => {
if (err) {
res.status(500).send(err.message);
} else if (redirectLocation) {
res.status(302).redirect(redirectLocation.pathname + redirectLocation.search);
} else if (renderProps) {
const html = ReactDOM.renderToString(React.createElement(Provider, { store: store },
React.createElement(Router.RouterContext, renderProps)
));
res.render('layout', {
html: html,
initialState: store.getState()
});
} else {
res.sendStatus(404);
}
});
});
// Production error handler
if (app.get('env') === 'production') {
app.use((err, req, res, next) => {
console.error(err.stack);
res.sendStatus(err.status || 500);
});
}
app.listen(app.get('port'), () => {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;