-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathserver.js
94 lines (79 loc) · 2.73 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
const Promise = require('bluebird');
const path = require('path');
const logger = require('morgan');
const express = require('express');
const bodyParser = require('body-parser');
const compression = require('compression');
const React = require('react');
const ReactDOM = require('react-dom/server');
const Router = require('react-router');
const nunjucks = require('nunjucks');
const postcss = require('postcss-middleware');
const cssnext = require('postcss-cssnext');
const atImport = require('postcss-import');
const dotenv = require('dotenv');
// Load Azure Storage environment variables
dotenv.load({ silent: true });
// App globals
global.__base = __dirname + '/';
global.__modules = {};
// Disable Bluebird warnings
Promise.config({ warnings: false });
// ES6 & ES7 Transpiler
require('babel-core/register');
require('babel-polyfill');
// Routes
const expressRoutes = require('./routes');
const reactRoutes = require('./website/routes');
const app = express();
app.set('port', process.env.PORT || 4000);
app.use(compression());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/css', postcss({
src: function(req) {
return path.join(__dirname, 'website', 'assets', 'css', req.path);
},
plugins: [atImport(), cssnext()]
}));
if (app.get('env') === 'development') {
const webpack = require('webpack');
const config = require('./webpack.config');
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
app.use(logger('dev'));
}
app.use(express.static(path.join(__dirname, 'website', 'assets'), { maxAge: 31557600000 }));
// Routes
app.post('/download', expressRoutes.download);
// Append numeric string to main.css and bundle.js
const cacheBust = Date.now();
// React server rendering
app.use(function(req, res) {
Router.match({ routes: reactRoutes.default, location: req.url }, function(err, redirectLocation, renderProps) {
if (err) {
res.status(500).send(err.message)
} else if (redirectLocation) {
res.status(302).redirect(redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
let html = ReactDOM.renderToString(React.createElement(Router.RouterContext, renderProps));
let page = nunjucks.render('index.html', {
html: html,
cacheBust: cacheBust
});
res.status(200).send(page);
} else {
res.status(404).send('Page Not Found')
}
});
});
app.listen(app.get('port'), function(err) {
console.log(`Express server listening on port ${app.get('port')} in ${app.get('env')} mode`);
});
process.on('unhandledRejection', function(reason, p) {
throw Error(reason);
});