This repository was archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathserver.js
120 lines (99 loc) · 2.87 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
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
import babelPolyfill from "babel-polyfill";
import koa from "koa";
import koaProxy from "koa-proxy";
import koaStatic from "koa-static";
import React from "react";
import ReactDOM from "react-dom/server";
import * as ReactRouter from "react-router";
import Transmit from "react-transmit";
import githubApi from "apis/github";
import routesContainer from "containers/routes";
import favicon from "favicon.ico";
try {
const app = koa();
const hostname = process.env.HOSTNAME || "localhost";
const port = process.env.PORT || 8000;
let routes = routesContainer;
app.use(koaStatic("static"));
app.use(koaProxy({
host: githubApi.url,
match: /^\/api\/github\//i,
map: (path) => path.replace(/^\/api\/github\//i, "/")
}));
app.use(function *(next) {
yield ((callback) => {
const webserver = __PRODUCTION__ ? "" : `//${this.hostname}:8080`;
const location = this.path;
ReactRouter.match({routes, location}, (error, redirectLocation, renderProps) => {
if (redirectLocation) {
this.redirect(redirectLocation.pathname + redirectLocation.search, "/");
return;
}
if (error || !renderProps) {
callback(error);
return;
}
const styles = {};
const StyleProvider = React.createClass({
childContextTypes:{
styles: React.PropTypes.array,
insertCss: React.PropTypes.func
},
getChildContext () {
return {
styles,
insertCss (style) { styles[style] = style._getCss(); }
};
},
render () {
return <ReactRouter.RouterContext {...this.props} />;
}
});
Transmit.renderToString(StyleProvider, renderProps).then(({reactString, reactData}) => {
let cssModules = "";
Object.keys(styles).forEach((style) => { cssModules += styles[style]; });
let template = (
`<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<title>react-isomorphic-starterkit</title>
<link rel="shortcut icon" href="${favicon}" />
<style>${cssModules}</style>
</head>
<body>
<div id="react-root">${reactString}</div>
</body>
</html>`
);
this.type = "text/html";
this.body = Transmit.injectIntoMarkup(template, reactData, [`${webserver}/dist/client.js`]);
callback(null);
}).catch(e => {
callback(e);
});
});
});
});
app.listen(port, () => {
console.info("==> ✅ Server is listening");
console.info("==> 🌎 Go to http://%s:%s", hostname, port);
});
if (__DEV__) {
if (module.hot) {
console.log("[HMR] Waiting for server-side updates");
module.hot.accept("containers/routes", () => {
routes = require("containers/routes");
});
module.hot.addStatusHandler((status) => {
if (status === "abort") {
setTimeout(() => process.exit(0), 0);
}
});
}
}
}
catch (error) {
console.error(error.stack || error);
throw error;
}