-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
59 lines (49 loc) · 1.7 KB
/
server.ts
File metadata and controls
59 lines (49 loc) · 1.7 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
48
49
50
51
52
53
54
55
56
57
58
59
import express from 'express'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const isProd = process.env.NODE_ENV === 'production'
const resolve = (p: string) => path.resolve(__dirname, p)
async function createServer() {
const app = express()
let vite: import('vite').ViteDevServer | undefined
if (!isProd) {
const { createServer: createViteServer } = await import('vite')
vite = await createViteServer({
server: { middlewareMode: true },
appType: 'custom',
})
app.use(vite.middlewares)
} else {
app.use(express.static(resolve('dist/client')))
}
app.use('*', async (req, res) => {
try {
const _url = req.originalUrl
let template: string
let render: (_url: string) => Promise<string>
if (!isProd) {
template = fs.readFileSync(resolve('index.html'), 'utf-8')
template = await vite!.transformIndexHtml(_url, template)
render = (await vite!.ssrLoadModule('/src/entry-server.tsx')).render
} else {
template = fs.readFileSync(resolve('dist/client/index.html'), 'utf-8')
render = (await import('./dist/server/entry-server.js')).render as (
_url: string,
) => Promise<string>
}
const appHtml = await render(_url)
const html = template.replace(`<!--ssr-outlet-->`, appHtml)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e: unknown) {
const error = e as Error
vite?.ssrFixStacktrace(error)
res.status(500).end(error.stack)
}
})
app.listen(5173, () => {
console.log('Server started at http://localhost:5173')
})
}
createServer()