-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
34 lines (27 loc) · 1.02 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
require('babel-register')({
"plugins":["transform-es2015-modules-commonjs"]
})
const express = require('express')
const fs = require('fs')
const path = require('path')
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const ReactRouter = require('react-router-dom')
// const compression = require('compression')
const app = express()
const App = require('./src/components/app.jsx').default
const StaticRouter = ReactRouter.StaticRouter
const PORT = 9000
app.use(express.static(path.resolve(__dirname, './public')))
// app.use(compression())
app.use((req, res)=> {
let context = {}
let htmlPath = path.resolve(__dirname, './public/index.html')
let baseTemplate = fs.readFileSync(htmlPath)
let body = ReactDOMServer.renderToString(React.createElement(
StaticRouter, {location: req.url, context}, React.createElement(App)
))
res.send(baseTemplate.toString().replace(`<div id='app'></div>`,
`<div id='app'>${body}</div>`))
})
app.listen(PORT, () => console.log('Now playing'))