This repository was archived by the owner on Sep 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
56 lines (46 loc) · 1.85 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
// Let's use ES6 on required files
require('babel/register');
var express = require('express');
var React = require('react');
var fs = require('fs');
var url = require('url');
var path = require('path');
var config = require('./config.json');
// Contentful client setup on the server
var contentful = require('./src/js/contentful');
contentful.init(config);
var initialDataLoader = require('./src/js/initialDataLoader');
var server = express();
server.get('/sw.js', function (req, res) {
res.sendFile('build/sw.js', {root: __dirname});
});
server.use('/js', express.static(path.join(__dirname, 'build', 'js')));
server.use('/css', express.static(path.join(__dirname, 'build', 'css')));
// We have only one HTML file and we'll let React take care of routing
// So we'll handle everything here that isn't a static resource
server.get('/*', function (req, res) {
var App = React.createFactory(require('./src/js/components/app'));
var navpath = url.parse(req.url).pathname;
// Initialize the main app component in the server with data for the requested route
initialDataLoader.handleServerData(navpath).then(function (initialData) {
res.type('html');
var client = App({path: navpath, initialData: initialData});
try {
var markup = React.renderToString(client);
var index = fs.readFileSync('build/index.html', 'utf-8');
// Quick and dirty templating
res.send(index
.replace('<!--MARKUP-->', markup)
.replace('<!--CONFIG-->', JSON.stringify(config))
.replace('<!--INITIALDATA-->', initialData ? JSON.stringify(initialData) : 'null')
);
} catch(err) {
var message = 'Rendering error: ' + err.message;
console.log(message);
console.log(err);
res.status(500).send(message);
}
});
});
console.log('Listening on ' + config.port);
server.listen(config.port);