-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (115 loc) · 3.56 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict'
const ENV = process.env.NODE_ENV || 'development';
require('babel-core/register')({
only: '/src/'
})
const Path = require('path')
const Boom = require('boom')
const Hapi = require('hapi')
const server = new Hapi.Server()
server.connection({
host: 'localhost',
port: 3012
})
const React = require('react')
const ReactDOM = require('react-dom/server')
const match = require('react-router').match
const RouterContext = React.createFactory(require('react-router').RouterContext)
const createMemoryHistory = require('react-router').createMemoryHistory
const configureStore = require('./src/store').configureStore
const routes = require('./src/routes').default
const Provider = React.createFactory(require('react-redux').Provider)
const syncHistoryWithStore = require('react-router-redux').syncHistoryWithStore
const Helmet = require('react-helmet')
if (ENV !== 'production') {
server.register(require('h2o2'), err => {
server.route({
method: 'GET',
path: '/static/{p*}',
handler: {
proxy: {
passThrough: true,
host: 'localhost',
port: 3011
}
}
})
})
}
server.register(require('inert'), err => {
server.route({
method: 'GET',
path: '/favicon.ico',
handler: function (request, reply) {
reply.file('./static/favicon.ico')
}
})
if (ENV === 'production') {
server.route({
method: 'GET',
path: '/static/{param*}',
handler: {
directory: {
path: Path.join(__dirname, 'static')
}
}
})
}
})
server.route({
method: 'GET',
path: '/{path*}',
handler: function (request, reply) {
const memoryHistory = createMemoryHistory(request.url.path)
const store = configureStore(memoryHistory)
const history = syncHistoryWithStore(memoryHistory, store)
match({ history, routes, location: request.url.path }, (err, redirect, props) => {
if (err) {
return reply(Boom.badImplementation(err.message))
}
if (redirect) {
return reply.redirect(redirect.pathname + redirect.search)
}
if (!props) {
return reply(Boom.notFound())
}
request.raw.res.setHeader('Content-Type', 'text/html; charset=utf-8')
request.raw.res.write(`<!doctype html>\n<html>`)
const routerContext = RouterContext(props)
const actions = routerContext.props.components
.filter(component => {
return component.WrappedComponent && component.WrappedComponent.dispatchActions
})
.map(component => {
return component.WrappedComponent.dispatchActions(store, routerContext.props)
})
Promise.all(actions)
.then(() => {
const provider = Provider({ store }, routerContext)
const content = ReactDOM.renderToString(provider)
const head = Helmet.rewind()
request.raw.res.write(`<head>
<meta charset="utf-8" />
${ head.title.toString() }
</head>`)
request.raw.res.write(`<body>
<div id="root">${ content }</div>
<script>window.__INITIAL_STATE__ = ${ JSON.stringify(store.getState()) }</script>
<script src="/static/bundle.js"></script>
</body>`)
request.raw.res.end(`</html>`)
})
.catch(err => {
console.log(err)
request.raw.res.write(`<div>Has Error</div>`)
request.raw.res.end(`</html>`)
})
})
}
})
server.start(err => {
if (err) {
console.log('error', err)
}
console.log('Server running at:', server.info.uri)
})