-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.js
44 lines (36 loc) · 1 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
const express = require('express')
import { matchRoutes } from 'react-router-config'
import Routes from './src/routes'
import renderer from './src/helpers/renderer'
import createStore from './src/helpers/serverStore'
const app = express()
app.use(express.static('dist'))
app.get('*', (req, res) => {
const store = createStore(req)
const promises = matchRoutes(Routes, req.path)
.map(({ route }) => {
return route.loadData ? route.loadData(store) : null
})
.map(promise => {
if (promise) {
return new Promise((resolve, reject) => {
promise.then(resolve).catch(resolve)
})
}
})
Promise.all(promises).then(() => {
const context = {}
const content = renderer(req, store, context)
if (context.url) {
return res.redirect(301, context.url)
}
if (context.notFound) {
res.status(404)
}
return res.send(content)
})
})
var port = process.env.PORT || 8080
app.listen(port, () => {
console.log('Listning on port 8080')
})