-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (69 loc) · 2.08 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
/*
* isomorphic-react-redux-starter-kit
*
* https://github.com/jostw/isomorphic-react-redux-starter-kit
*
* Copyright (c) 2016 jos
* Licensed under the MIT license.
*/
'use strict';
import express from 'express';
import browserSync from 'browser-sync';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { match } from 'react-router';
import { port } from './config';
import { routesConfig } from './js/app/config';
import routes from './js/app/routes';
import fetch from 'isomorphic-fetch';
import Index from './js/components/Index.jsx';
const isDev = process.argv.some(arg => arg.match('dev'));
const app = express();
let requestCount;
let requestTime;
app.get(routesConfig.home.path, (req, res, next) => {
if (!req.xhr) {
next();
return;
}
requestCount++;
res.send({ time: requestCount > 2 ? new Date() : requestTime });
});
app.get(routesConfig.about.path, (req, res, next) => {
if (!req.xhr) {
next();
return;
}
requestCount++;
res.send({ time: requestCount > 2 ? new Date() : requestTime });
});
app.use(express.static('public'));
app.use((req, res) => {
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
const url = 'http://' + req.headers.host + req.url;
requestCount = 0;
requestTime = new Date();
fetch(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => response.json())
.then(json => {
const index = React.createElement(Index, { isDev, renderProps, initialState: json });
res.send('<!DOCTYPE html>' + ReactDOMServer.renderToStaticMarkup(index));
});
});
});
/* eslint no-console: 0 */
app.listen(port.app, () => {
console.log('Server listening on port', port.app);
if (isDev) {
const browserSyncServer = browserSync.create();
browserSyncServer.init({
logSnippet: false,
reloadOnRestart: true,
port: port.browserSync
});
}
});