-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
130 lines (109 loc) · 3.57 KB
/
server.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import fs from 'fs'
import path from 'path'
import express from 'express'
import playlist from './src/api'
import serveStatic from 'serve-static'
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
async function createServer(
root = process.cwd(),
isProd = process.env.NODE_ENV === 'production'
) {
const resolve = (p: string) => path.resolve(__dirname, p)
const indexProd = isProd
? fs.readFileSync(resolve('client/index.html'), 'utf-8')
: ''
const app = express()
/**
* @type {import('vite').ViteDevServer}
*/
let vite: any
if (!isProd) {
vite = await require('vite').createServer({
root,
logLevel: isTest ? 'error' : 'info',
server: {
middlewareMode: 'ssr',
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100
}
}
})
// use vite's connect instance as middleware
app.use(vite.middlewares)
} else {
app.use(serveStatic('dist/client', { index: false }))
// console.log('-----------------------------_>')
// app.use(require('compression')())
// app.use(
// require('serve-static')(resolve('dist/client'), {
// index: false
// })
// )
}
app.use('/api/playlist/hot', playlist.hot)
app.use('/api/playlist/recommend', playlist.recommend)
app.use('*', async (req, res) => {
console.log(',,,,,,')
try {
const url = req.originalUrl
let template: string;
let render: Function;
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.tsx')).render
} else {
template = indexProd
render = require('./server/entry-server.js').render
}
const context = {}
// if (context.url) {
// // Somewhere a `<Redirect>` was rendered
// return res.redirect(301, context.url)
// }
if (req.query.csr !== undefined) {
return res.status(200).set({ 'Content-Type': 'text/html' }).end(template)
}
let sagas;
let configureStore;
if (!isProd) {
sagas = (await vite.ssrLoadModule('/src/shared/saga')).default
configureStore = (await vite.ssrLoadModule('/src/entry-server.tsx')).configureStore
} else {
sagas = require(('./src/shared/saga')).default
configureStore = require('./src/entry-server.js').configureStore
}
const store = configureStore()
// const context = {}
const aysncSaga = await store.runSaga(sagas)
const appHtml = await render(url, store, context)
aysncSaga.toPromise().then(async () => {
const preloadedState = store.getState()
const html = template.replace(`<!--app-html-->`, appHtml).replace(`<!--app-state-->`, `<script>window.__PRELOADED_STATE__=${JSON.stringify(preloadedState)}</script>`)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
}, () => {
// todo
})
store.close()
} catch (e) {
!isProd && vite.ssrFixStacktrace(e)
console.log(e.stack)
res.status(500).end(e.stack)
}
})
return { app, vite }
}
if (!isTest) {
createServer().then(({ app }) => {
app.listen(8000, () => {
console.log('http://localhost:8000')
})
}
)
}
// for test use
exports.createServer = createServer