This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
182 lines (155 loc) · 4.5 KB
/
app.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
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);
});
var config = require('./config/default.js');
if (config.newrelic_enabled) {
require('newrelic');
require('@newrelic/native-metrics');
}
var http = require('http'),
mongoose = require('./server/db/index.js').mongoose,
morgan = require('morgan'),
express = require('express'),
app = express(),
server = http.createServer(app),
session = require('express-session'),
bodyParser = require('body-parser'),
MongoStore = require('connect-mongo')(session),
csrf = require('csurf'),
csrfProtection = csrf(), // eslint-disable-line
apiRouter = require('./server/routes/api.js'),
indexRouter = require('./server/routes/index.js'),
shortenerRouter = require('./server/routes/shortener.js');
if (config.development) {
mongoose.set('debug', true);
}
// Force https
app.use(function(req, res, next) {
if (
!req.secure &&
req.get('x-forwarded-proto') !== 'https' &&
app.get('env') !== 'development'
) {
if (config.service === 'shortener') {
return res.redirect(config.shortener_host + req.url);
} else {
return res.redirect(config.host + req.url);
}
}
next();
});
// Logging
morgan.token('remote-addr', function(req) {
return 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'
})
);
// Server side views (if used)
app.set('view engine', 'pug');
app.set('views', './server/views');
app.disable('view cache');
if (app.get('env') !== 'production') {
// Pretty API Responses
app.set('json spaces', config.api_spaces);
}
// Disable x-powered-by
app.disable('x-powered-by');
// Initialize session
var sessionStore = new MongoStore({
url: 'mongodb://' + config.mongo_hostname + '/' + config.sessions_db
});
var 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(function(req, res, next) {
if (req.csrfToken) {
res.cookie('xsrf-token', req.csrfToken());
}
return next();
});
app.get('/apple-app-site-association', function(req, res) {
res.send({
webcredentials: {
apps: config.site_association
}
});
});
// Other route middleware (modules in `routes/`)
if (config.service === 'shortener') {
app.use('/', shortenerRouter);
} else {
app.use('/', indexRouter);
app.use('/v1', apiRouter);
// Intiialize development webpack (hot reloading, etc);
if (app.get('env') !== 'production' && !config.api_work) {
var webpack = require('webpack'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
historyApiFallback = require('connect-history-api-fallback'),
webpackConfig = require('./webpack.dev.config'),
webpackCompiler = webpack(webpackConfig),
webpackMiddlewareInstance = webpackDevMiddleware(webpackCompiler, {
publicPath: webpackConfig.output.publicPath,
stats: {
colors: true
}
});
app.use(webpackMiddlewareInstance);
app.use(
historyApiFallback({
disableDotRule: true
})
);
app.use(webpackMiddlewareInstance);
app.use(
webpackHotMiddleware(webpackCompiler, {
log: console.log
})
);
} else {
// Static files middleware
app.use(express.static('build'));
app.use(function(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
};