-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (37 loc) · 1.28 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
require('babel-register')
const express = require('express')
const compression = require('compression')
const React = require('react')
const ReactDOMServer = require('react-dom/server')
const ReactRouter = require('react-router-dom')
const StaticRouter = ReactRouter.StaticRouter
const _ = require('lodash')
const favicon = require('serve-favicon')
const path = require('path')
const fs = require('fs')
const baseTemplate = fs.readFileSync('./index.html')
const template = _.template(baseTemplate)
require('dotenv').config()
// entry point
const AppClient = require('./src/Routes').default
const apiRoutes = require('./api/apiRoutes')
const server = express()
server.use(compression())
server.use(favicon(path.join(__dirname, 'public', 'favicon', 'favicon.ico')))
server.use('/', express.static(path.join(__dirname, 'public'), {maxage: 1210000000}))
server.use('/api', apiRoutes)
// server side rendering
server.use((req, res) => {
const context = {}
const body = ReactDOMServer.renderToString(
React.createElement(StaticRouter, {location: req.url, context: context},
React.createElement(AppClient)
)
)
res.write(template({body: body}))
res.end()
})
// server port and setup
const port = process.env.PORT || 8080
console.log('Server hosted on port ' + port)
server.listen(port)