-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
32 lines (25 loc) · 943 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
32
'use strict'
import path from 'path'
import express from 'express'
import compression from 'compression'
import morgan from 'morgan'
import React from 'react'
import { renderToString } from 'react-dom/server'
import { match, RouterContext } from 'react-router'
import DemoPageRoutes from './DemoPage/Routes'
const app = express()
const staticPath = path.join(__dirname, '/dist/')
const port = Number(process.env.PORT || 8000)
app.use(compression())
app.use(morgan())
const sendStaticFile = name =>
(req, res) => {
match({ routes: DemoPageRoutes, location: req.path }, (_, redirectLocation, renderProps) => {
let content = renderToString(React.createElement(RouterContext, renderProps))
res.render(staticPath + name, { content })
})
}
app.use(express.static(staticPath))
app.get('/', sendStaticFile('index.ejs'))
app.get('*', sendStaticFile('index.ejs'))
app.listen(port, console.log.bind(this, 'Port: ' + port))