This repository has been archived by the owner on Mar 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
app.js
200 lines (163 loc) · 5.73 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var habitat = require('habitat');
habitat.load('.env');
var path = require('path'),
fs = require('fs'),
express = require('express'),
helmet = require('helmet'),
url = require('url'),
React = require('react'),
ReactRouter = require('react-router'),
Router = ReactRouter.Router,
match = ReactRouter.match,
routington = require('routington'),
PORT = process.env.PORT || 8008,
PRODUCTION = (process.env.NODE_ENV === 'production'),
DIST_DIR = path.join(__dirname, 'dist'),
CODEMOJI_URL = process.env.CODEMOJI_URL || "https://codemoji.mofostaging.net",
localize = require('mofo-localize'),
urlToRoutePath = require('./server/url-to-route-path'),
renderComponentPage = require('./server/render-component-page'),
WpPageChecker = require('./lib/wp-page-checker'),
config = require('./config/config'),
SUPPORTED_LOCALES = config.SUPPORTED_LOCALES,
MAKER_PARTY_LOCALES = config.MAKER_PARTY_LOCALES,
locale = "";
// the static HTML generator
var serverBundle = require('./build/server.library');
var router = React.createElement(Router, {routes: serverBundle.routes});
var matcher;
if (process.env.NODE_ENV !== 'production') {
var requireUncached = require('require-uncached'),
chokidar = require('chokidar');
// reload our index and router if there's a change to the static site generator code
chokidar.watch('./build').on('all', function(_event, _path) {
serverBundle = requireUncached('./build/server.library');
router = React.createElement(Router, {routes: serverBundle.routes});
});
}
var app = express();
/**
* Several app security settings
*/
app.disable('x-powered-by');
var securityHeaders = require('./server/security-headers');
app.use(helmet.contentSecurityPolicy(securityHeaders));
app.use(helmet.xssFilter({
setOnOldIE: true
}));
// maxAge for HSTS header must be at least 18 weeks (see https://hstspreload.org/)
app.use(helmet.hsts({
maxAge: 60 * 60 * 24 * 7 * 18 // 18 weeks in seconds
}));
app.use(helmet.ieNoOpen());
app.use(helmet.noSniff());
/**
* Wait for the router to come online.
*/
app.use(function(req, res, next) {
if (router) {
return next();
}
res.send('Please wait while the server-side bundle regenerates.');
});
/**
* If we have a router, check if we're dealing with a redirect.
*/
app.use(function(req, res, next) {
var routePath = urlToRoutePath(req.path);
if (!serverBundle.REDIRECTS[routePath]) {
return next();
}
res.redirect('/' + serverBundle.REDIRECTS[routePath] + '/');
});
/**
* If it's not a redirect, is it a component page?
*/
app.use(function(req, res, next) {
var routes = serverBundle.routes,
location = urlToRoutePath(req.url),
urls = serverBundle.URLS,
lang;
if (!matcher) {
matcher = routington();
urls.forEach(function(route) { matcher.define(route); });
}
match({ routes: routes, location: location}, function resolveRoute(err, redirect, props) {
// React router based redirect? (routes.jsx)
if(redirect) {
return res.redirect(redirect.pathname);
}
// is this even a component?
if ( !props ) {
return next();
}
// if this belongs to one of the predefined urls, let's generate its associated page
if ( matcher.match(location) ) {
var search = url.parse(req.url).search || "";
locale = localize.parseLocale(req.headers["accept-language"], location, SUPPORTED_LOCALES).locale;
if (location === "/") {
res.redirect(302, location + locale + search);
return;
}
lang = location.split('/')[0];
return renderComponentPage(serverBundle, location, res, lang);
} else {
// if neither of those, this is wordpress content...
// check to see a page with this slug exists on the WordPress site
WpPageChecker(location, function(error, wpContent) {
if ( error ) {
return next();
}
// WP page exists, let's load the WordPress content through a component page
return renderComponentPage(serverBundle, location, res);
});
}
});
});
/**
* codemoji redirect - https://github.com/mozilla/codemoji
*/
app.use('/codemoji', function(req, res) {
var location = url.parse(CODEMOJI_URL);
location.pathname = req.path;
location.query = req.query;
res.redirect(307, url.format(location));
});
/**
* Is this a static asset?
*/
app.use(express.static(DIST_DIR));
/**
* Maybe it is a route, but needs the localized path
*/
app.use(function(req, res, next) {
var location = url.parse(req.url).pathname,
// Useful locales changes if we're on maker party pages
supportedLocales = location.match(/\/events(\/resources)?$/) ? SUPPORTED_LOCALES.concat(MAKER_PARTY_LOCALES) : SUPPORTED_LOCALES,
search = url.parse(req.url).search || "",
parsed = localize.parseLocale(req.headers["accept-language"], location, supportedLocales),
parsedLocale = parsed.locale,
parsedRedirect = parsed.redirect;
// See if we should redirect.
if (parsedRedirect) {
res.redirect(307, "/" + parsedLocale + parsedRedirect + search);
} else {
next();
}
});
app.use(require('./server/404'));
app.READY_STRING = "Server listening on port";
app.PORT = PORT;
var startProdApp = function() {
app.listen(PORT, function() {
console.log('\n\n==================================================');
console.log('= =');
console.log('= ', app.READY_STRING, PORT, ' =');
console.log('= =');
console.log('==================================================\n');
});
};
app.startProdApp = startProdApp;
module.exports = app;
// Auto-start only if this was not a require() call for app.js
if (!module.parent) { startProdApp(); }