-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
31 lines (26 loc) · 873 Bytes
/
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
require('babel-register')
const express = require('express')
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const ReactRouter = require('react-router')
const ServerRouter = ReactRouter.ServerRouter
const _ = require('lodash')
const fs = require('fs')
const port = 5050
const baseTemplate = fs.readFileSync('./index.html')
const template = _.template(baseTemplate)
const App = require('./js/App').default
const server = express()
server.use('/public', express.static('./public'))
server.use((req, res) => {
const context = ReactRouter.createServerRenderContext()
let body = ReactDOMServer.renderToString(
React.createElement(ServerRouter, {location: req.url, context: context},
React.createElement(App)
)
)
res.write(template({body: body}))
res.end()
})
console.log('listening on ' + port)
server.listen(port)