-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
server.js
39 lines (33 loc) · 978 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
33
34
35
36
37
38
39
import express from 'express'
import proxy from 'express-http-proxy'
import { h } from 'preact'
import render from 'preact-render-to-string'
import { RendererProvider } from 'preact-fela'
import { renderToMarkup } from 'fela-dom'
import fs from 'fs'
import App from './app.js'
import createRenderer from './renderer'
const app = express()
app.use(
'/bundle.js',
proxy('localhost:8080', { forwardPath: () => '/bundle.js' })
)
app.get('/', (req, res) => {
const renderer = createRenderer()
const indexHTML = fs.readFileSync(`${__dirname}/index.html`).toString()
const appHtml = render(
<RendererProvider renderer={renderer}>
<App />
</RendererProvider>
)
const appCSS = renderToMarkup(renderer)
res.write(
indexHTML
.replace('<!-- {{app}} -->', appHtml)
.replace('<!-- {{css}} -->', appCSS)
)
res.end()
})
app.listen(8000, 'localhost', () => {
console.log('Access the universal app at http://%s:%d', 'localhost', 8000)
})