-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (40 loc) · 1.17 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
45
46
47
48
import 'dotenv/config'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import express from 'express'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
async function createDevServer() {
const app = express()
const vite = await (
await import('vite')
).createServer({
server: { middlewareMode: true },
appType: 'custom',
})
app.use(vite.middlewares)
app.use('*', async (req, res, next) => {
try {
const templateHtml = fs.readFileSync(
path.resolve(__dirname, 'index.html'),
'utf-8',
)
const template = await vite.transformIndexHtml(
req.originalUrl,
templateHtml,
)
const { render } = await vite.ssrLoadModule('/src/entry-server.jsx')
const appHtml = await render(req)
const html = template.replace(`<!--ssr-outlet-->`, appHtml)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (err) {
vite.ssrFixStacktrace(err)
next(err)
}
})
return app
}
const app = await createDevServer()
app.listen(process.env.PORT, () => {
console.log(`Server running at http://localhost:${process.env.PORT}`)
})