forked from svnm/react-webpack-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverProd.js
More file actions
65 lines (56 loc) · 1.4 KB
/
Copy pathserverProd.js
File metadata and controls
65 lines (56 loc) · 1.4 KB
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
require('babel-register');
import express from 'express'
import React from 'react'
import { renderToString } from 'react-dom/server'
import path from 'path'
import Demo from './src/components/Demo'
const app = express()
const ip = 'localhost'
const port = 3000
/* Serve static */
app.use('/public', express.static(__dirname + '/public'))
/* Server render */
app.use(handleRender)
function App(props) {
return (
<main>
<Demo />
</main>
)
}
function handleRender(req, res) {
const html = renderToString(<App />)
res.send('<!doctype html>\n' + renderToString(<HTML html={html} />))
}
const HTML = ({ html }) => (
<html>
<head>
<link rel='stylesheet' type='text/css' href='/public/style.css' />
</head>
<body>
<div id='root' dangerouslySetInnerHTML={{ __html: html }}/>
<script src='/public/vendor.js' />
<script src='/public/bundle.js' />
</body>
</html>
)
function renderPage(html) {
return `
<!doctype html>
<html>
<head>
<title>React Example</title>
<link rel='stylesheet' type='text/css' href='/public/style.css' />
</head>
<body>
<div id="root">${html}</div>
<script src='/public/vendor.js' />
<script src='/public/bundle.js' />
</body>
</html>
`
}
app.listen(port, ip, function (err) {
if (err) { console.log(err); return; }
console.log(`listening on ${ip}:${port}`)
})