forked from mozilla/learning.mozilla.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
139 lines (122 loc) · 3.63 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
var path = require('path');
var fs = require('fs');
var express = require('express');
var React = require('react');
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var match = ReactRouter.match;
var indexStaticWatcher = require('./lib/build/index-static-watcher').create();
var PORT = process.env.PORT || 8008;
var PRODUCTION = (process.env.NODE_ENV === 'production');
var DIST_DIR = path.join(__dirname, 'dist');
var indexStatic;
var router;
var app = express();
var notFoundHTML = [
'<!doctype html>',
'<html><head>',
'<meta charset="utf-8">',
'<title>404 - Page not found</title>',
'</head><body>',
'<p>404 - Page not found</p>',
'</body></html>'
].join('');
var urlToRoutePath = function(loc) {
if (loc !== '/') {
loc = loc.replace(/^\//, '').replace(/\/$/, '');
}
return loc;
};
var startProdApp = function() {
console.log([
'Production mode enabled. Note that "npm install" is assumed to',
'have recently been run with NODE_ENV="production". If this is not',
'the case, some or all static assets may be out of date.'
].join('\n'));
indexStaticWatcher.build(function(err, newIndexStatic) {
if (err) {
throw err;
}
console.log('Built server-side bundle.');
updateIndexStatic(newIndexStatic);
app.listen(PORT, function() {
console.log('Listening on port', PORT);
});
});
};
var updateIndexStatic = function(newIndexStatic) {
indexStatic = newIndexStatic;
router = indexStatic ? React.createElement(Router, {routes: indexStatic.routes}) : null;
};
// make sure the dir we'll be using for static hosting exists.
if (!fs.existsSync(DIST_DIR)) {
fs.mkdirSync(DIST_DIR);
}
/**
* 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 url = urlToRoutePath(req.path);
if (!indexStatic.REDIRECTS[url]) {
return next();
}
res.redirect('/' + indexStatic.REDIRECTS[url] + '/');
});
/**
* If it's not a redirect, is it a component page?
*/
app.use(function(req, res, next) {
var routes = indexStatic.routes;
var location = urlToRoutePath(req.url);
match({ routes: routes, location: location}, function resolveRoute(err, redirect, props) {
// this is a valid component: generat its associated page
if (props) {
indexStatic.generate(location, {}, function(err, location, title, html) {
if (err) {
return next(err);
}
return res.type('html').send(html);
});
}
// this is not a specific component - although it might be if we massage the path:
else {
location = urlToRoutePath(req.path) + '/';
match({ routes: routes, location: location}, function resolvePath(err, redirect, props) {
if (redirect || props) {
// this will work, as long as we rewrite the path
return res.redirect(req.path + '/');
}
// this is not a url that can be services by React. Try more middleware.
return next();
});
}
});
});
/**
* Last chance: is this a static asset?
*/
app.use(express.static(DIST_DIR));
app.DIST_DIR = DIST_DIR;
app.updateIndexStatic = updateIndexStatic;
module.exports = app;
if (!module.parent) {
console.log('Initializing server.');
if (PRODUCTION) {
startProdApp();
} else {
console.log([
'This server can only be run as a script when NODE_ENV="production".',
'To run it in development mode, please use "npm run app".'
].join('\n'));
process.exit(1);
}
}