This repository has been archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (83 loc) · 2.45 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
const Koa = require('koa');
import { renderToString } from 'react-dom/server'
import React from 'react'
import App from './src/containers/App'
let hash = null;
// Redux related imports
import { createStore } from 'redux'
import reducers from './src/reducers'
import { Provider } from 'react-redux'
import { ServerRouter, createServerRenderContext } from 'react-router'
console.log(`Build hash ${ hash } will be used for this instance.`);
function setHash(_h) {
hash = _h;
}
function makeServer(devMode = false) {
const app = new Koa();
/**
* Serve static assets with Koa.
* In an actual deployment, this would better be handled by nginx
* or some other http server optimized for serving static assets.
* For a simple development experience, we will just use Koa.
*/
app.use(require('koa-static')('www'))
app.use(ctx => {
const store = createStore(reducers);
const context = createServerRenderContext();
const markup = renderToString(
<ServerRouter
location={ctx.url}
context={context}
>
<Provider store={store}>
<App />
</Provider>
</ServerRouter>
)
const result = context.getResult();
if(result.redirect) {
// TODO: Handle redirect here
} else if(result.missed) {
// TODO: Handle 404s here
}
// Get redux state
const initialState = store.getState();
// Serve dev assets only in dev
// Todo: use hostname instead of assuming localhost
const bundleURL = devMode ? `http://localhost:3000/main.${ hash }.js` : `/assets/main.${ hash }.js`;
ctx.body = `<!DOCTYPE html>
<html>
<head>
<title>PHQ-9 Demo</title>
<link rel="stylesheet" href="/assets/all.css" />
<script>
<!--
window.rdx_init = ${ JSON.stringify(initialState) }
--></script>
</head>
<body>
<div id="rct_root">${ markup }</div>
<script src="${bundleURL}"></script>
</body>
</html>`
})
const port = 5000;
app.listen(port)
console.log(`App is listening on port ${ port }`)
return app;
}
export {
makeServer,
setHash,
}
if(!module.parent) {
console.log('Running standalone production build.');
const fs = require('fs');
setHash(fs.readFileSync('./build'));
// Die if hash is not correct
if(hash.length !== 20) {
console.log(`Unexpected hash (${ hash }) found. Please run "npm start".`);
process.exit(1);
}
makeServer();
}