-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.react.js
42 lines (32 loc) · 1008 Bytes
/
server.react.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
// @ts-check
const fs = require('fs')
const path = require('path')
const express = require('express')
const resolve = (p) => path.resolve(__dirname, p)
const manifest = require('./dist/vite-react/client/ssr-manifest.json')
const app = express()
/**
* @type {import('vite').ViteDevServer}
*/
app.use(require('compression')())
app.use(
"/assets",
express.static(path.join(__dirname, "./dist/vite-react/client", "assets"))
);
app.use('*', async (req, res) => {
try {
const url = req.originalUrl
const template = fs.readFileSync(resolve('dist/vite-react/client/index.html'), 'utf-8')
const render = require('./dist/vite-react/server/entry-server.js').render
const appHtml = await render(url)
const html = template
.replace(`<!--ssr-outlet-->`, appHtml)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
console.log(e.stack)
res.status(500).end(e.stack)
}
})
app.listen(3000, () => {
console.log('http://localhost:3000')
})