-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (39 loc) · 1 KB
/
index.js
File metadata and controls
47 lines (39 loc) · 1 KB
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
47
import Koa from 'koa'
import { readFileSync } from 'node:fs'
import { template } from 'lodash-es'
import KoaStatic from 'koa-static'
import { renderAsyncFragments } from '@riotjs/ssr'
import { pathToFileURL } from 'node:url'
import { register } from 'node:module'
register('@riotjs/register', pathToFileURL('./'))
const page = readFileSync('./index.html', 'utf8')
const app = new Koa()
const pages = [
{
path: '/',
label: 'Home',
component: 'home',
},
{
path: '/about',
label: 'About',
component: 'about',
},
]
app.use(KoaStatic('./public'))
app.use(async (ctx) => {
const initialState = {
initialRoute: ctx.request.url,
base: 'http://localhost:3000',
pages,
}
const { default: App } = await import('./app/app.riot')
const { html, css } = await renderAsyncFragments('app', App, initialState)
ctx.body = template(page)({
html,
initialState: JSON.stringify(initialState),
css,
})
})
app.listen(3000)
console.log('App running on: http://localhost:3000')