-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (119 loc) · 3.31 KB
/
app.js
File metadata and controls
142 lines (119 loc) · 3.31 KB
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
process.on('unhandledRejection', (error) => {
console.log('unhandledRejection', error);
});
process.on('warning', (warning) => {
console.log('warning', warning);
});
process.on('uncaughtException', (exception) => {
console.log('uncaughtException', exception);
});
process.on('rejectionHandled', (rejection) => {
console.log('rejectionHandled', rejection);
});
const config = require('./config/default.js');
const http = require('http');
const { mongoose } = require('./server/db/index.js');
const morgan = require('morgan');
const express = require('express');
const app = express();
const server = http.createServer(app);
const session = require('express-session');
const bodyParser = require('body-parser');
const MongoStore = require('connect-mongo')(session);
const csrf = require('csurf');
const csrfProtection = csrf(); // eslint-disable-line
const apiRouter = require('./server/routes/api.js');
const indexRouter = require('./server/routes/index.js');
// Force https
app.use((req, res, next) => {
if (
!req.secure &&
req.get('x-forwarded-proto') !== 'https' &&
app.get('env') !== 'development'
) {
return res.redirect(config.host + req.url);
}
next();
});
// Logging
morgan.token('remote-addr', (req) => req.headers['x-forwarded-for'] || req.ip);
app.use(morgan('combined'));
// Request parsers
app.use(
bodyParser.urlencoded({
extended: true,
limit: '50mb',
}),
);
app.use(
bodyParser.json({
limit: '50mb',
}),
);
// Pretty API Responses
app.set('json spaces', 4);
// Disable x-powered-by
app.disable('x-powered-by');
// Initialize session
const sessionStore = new MongoStore({
url: `mongodb://${config.mongo_hostname}/${config.sessions_db}`,
});
const sessionMiddleware = session({
secret: config.secret,
store: sessionStore,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: config.token_expiration * 24 * 60 * 60 * 1000,
},
});
app.use(sessionMiddleware);
// Set an xsrf-token for the session if it's enabled
app.use((req, res, next) => {
if (req.csrfToken) {
res.cookie('xsrf-token', req.csrfToken());
}
return next();
});
app.use('/', indexRouter);
app.use('/v1', apiRouter);
// Intiialize development webpack (hot reloading, etc);
if (app.get('env') !== 'production' && !config.api_work) {
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const historyApiFallback = require('connect-history-api-fallback');
const webpackConfig = require('./webpack.dev.config');
const webpackCompiler = webpack(webpackConfig);
const webpackMiddlewareInstance = webpackDevMiddleware(webpackCompiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true,
},
});
app.use(webpackMiddlewareInstance);
app.use(historyApiFallback());
app.use(webpackMiddlewareInstance);
app.use(
webpackHotMiddleware(webpackCompiler, {
log: console.log,
}),
);
} else {
// Static files middleware
app.use(express.static('build'));
app.use((req, res) => {
res.sendFile(`${__dirname}/build/index.html`);
});
}
require('./server/interactors/setup.js');
// Now we start the server
if (config.start_server) {
server.listen(config.server_port);
}
module.exports = {
mongoose,
express,
app,
server,
};