This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
115 lines (100 loc) · 3.26 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const fs = require('fs')
const path = require('path')
const express = require('express')
const cookieParser = require('cookie-parser')
const { createProxyMiddleware } = require('http-proxy-middleware')
const { proxy, port } = require('./config/ssr.config')
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
const isStag = process.env.NODE_ENV === 'stag'
async function createServer (
root = process.cwd(),
isProd = process.env.NODE_ENV === 'production' || isStag
) {
const resolve = (p) => path.resolve(__dirname, p)
const indexProd = isProd
? fs.readFileSync(resolve('dist/client/index.html'), 'utf-8')
: ''
const manifest = isProd
? require('./dist/client/ssr-manifest.json')
: {}
const app = express()
app.use(cookieParser())
if (!isProd || (isProd && isStag)) {
for (const p in proxy) {
app.use(p, createProxyMiddleware(proxy[p]))
}
}
/**
* @type {import('vite').ViteDevServer}
*/
let vite
if (!isProd) {
vite = await require('vite').createServer({
root,
logLevel: isTest ? 'error' : 'info',
server: {
middlewareMode: true
}
})
// use vite's connect instance as middleware
app.use(vite.middlewares)
} else {
app.use(require('compression')())
app.use(
require('serve-static')(resolve('dist/client'), {
index: false
})
)
}
app.use('*', async (req, res) => {
const context = {
// for nginx, set config:
// proxy_set_header X-Forwarded-Proto $scheme
// proxy_set_header Host $host
// host: `${req.headers['x-forwarded-proto']}://${req.headers.host}`,
host: `${req.protocol}://${req.headers.host}`,
ua: req.headers['user-agent']
}
try {
const url = req.originalUrl
let template, render
if (!isProd) {
// always read fresh template in dev
template = fs.readFileSync(resolve('index.html'), 'utf-8')
template = await vite.transformIndexHtml(url, template)
render = (await vite.ssrLoadModule('/src/entry-server.js')).render
} else {
template = indexProd
render = require('./dist/server/entry-server.js').render
}
const [err, appHtml, preloadLinks, syncState, headTags, htmlAttrs, bodyAttrs] = await render(url, manifest, context)
const html = template
.replace('data-html-attrs', htmlAttrs)
.replace('<!--head-tags-->', headTags)
.replace('data-body-attrs', bodyAttrs)
.replace('<!--preload-links-->', preloadLinks)
.replace('<!--ssr-outlet-->', appHtml)
.replace('/*sync-state-outlet*/', `window.__syncState__ = ${JSON.stringify(syncState)}`) // 注入同步数据
let statusCode = 200
if (err) {
console.log(err)
statusCode = err.message.indexOf('404') === 0 ? 404 : 202 // 渲染错误用202不被缓存
}
res.status(statusCode).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
vite && vite.ssrFixStacktrace(e)
console.log(e.stack)
res.status(500).end(e.stack)
}
})
return { app, vite }
}
if (!isTest) {
createServer().then(({ app }) =>
app.listen(port, () => {
console.log(`http://localhost:${port}`)
})
)
}
// for test use
exports.createServer = createServer