-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (38 loc) · 1.26 KB
/
app.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
import express from 'express'
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import { fetchData } from './modules/apiClient'
import Layout from './components/Layout.jsx'
import Content from './components/Content/Content.jsx'
const app = express()
// eslint-disable-next-line no-process-env
const env = process.env.NODE_ENV || 'development'
if (env === 'development') {
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const webpack = require('webpack')
const compiler = webpack(require('./webpack/development.config'))
app.use(webpackHotMiddleware(compiler))
app.use(webpackDevMiddleware(compiler, {
// options
stats: {
colors: true,
},
}))
}
app.get('/', (req, res) => {
// make 'fake' http request and render component Layout with its data
fetchData().then((data) => {
const props = { title: data.title }
const markup = ReactDOMServer.renderToString(<Content {...props} />)
const content = {
props,
markup,
component: 'Content',
}
const html = ReactDOMServer.renderToStaticMarkup(<Layout content={content} />)
res.status(200).send(html)
})
})
app.use(express.static('static', {}))
export default app