-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (100 loc) · 3.13 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
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const path = require('path');
const server = require('http').createServer(app);
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const expressValidator = require('express-validator');
const MongoStore = require('connect-mongo')(session);
const passport = require('passport');
const history = require('connect-history-api-fallback');
const morgan = require('morgan');
const io = require('socket.io').listen(server);
const SECRETS = require('./server/config/secrets');
let config = '';
const env = process.env.NODE_ENV;
console.log(env);
/* eslint-disable global-require */
if (env !== 'production') {
config = require('./webpack.config.dev');
console.log('Launching dev version');
} else {
config = require('./webpack.config.prod');
console.log('Launching production version');
}
/* eslint-enable global-require */
require('./server/config/sockets')(io);
app.use(history());
// Parse cookies
app.use(cookieParser());
// Express session and validation error
app.use(session({
secret: SECRETS.sessionSecret,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
cookie: { maxAge: 360 * 60 * 1000 },
}));
// Parse application/json
app.use(bodyParser.json());
// Body parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator({
errorFormatter: (param, msg, value) => {
const namespace = param.split('.');
const root = namespace.shift();
let formParam = root;
while (namespace.length) {
formParam += `[${namespace.shift()}]`;
}
return {
param: formParam,
msg: msg,
value: value,
};
},
}));
// Log request's to console
if (process.env.NODE_ENV === 'production') {
app.use(morgan('combined'));
} else {
app.use(morgan('dev'));
}
// Initialize passport
require('./server/config/pass')(passport);
app.use(passport.initialize());
app.use(passport.session());
// Server routes
const messages = require('./server/routes/messages');
const users = require('./server/routes/users');
app.use('/users', users);
app.use('/', messages);
// Webpack middleware config
if (process.env.NODE_ENV !== 'production') {
const webpack = require('webpack');
const webpackMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const compiler = webpack(config);
app.use(webpackMiddleware(compiler, {
hot: true,
noInfo: true,
reload: true,
publicPath: config.output.publicPath,
}));
app.use((webpackHotMiddleware)(compiler));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './index.html'));
});
} else {
// Get static files.
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (req, res) => {
res.sendFile('index.html', { root: path.join(__dirname, 'dist') });
});
}
// Create server
const port = process.env.PORT || '3002';
app.set('port', port);
server.listen(port, () => console.log(`API running on localhost:${port}`));