forked from cafe-labs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
54 lines (44 loc) · 1.09 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
import { consola } from 'consola'
import express from 'express'
import httpProxy from 'http-proxy'
import wisp from 'wisp-server-node'
import http from 'node:http'
import path from 'node:path'
import { build } from 'vite'
import { Socket } from 'node:net'
const httpServer = http.createServer()
const proxy = httpProxy.createProxyServer()
const app = express()
const port = process.env.PORT || 3003
consola.start("Building frontend")
await build()
app.use(express.static('dist'))
app.use('/cdn', (req, res) => {
proxy.web(req, res, {
target: 'https://assets.3kh0.net',
changeOrigin: true,
// @ts-ignore
rewritePath: {
'^/cdn': ''
}
})
})
app.get('*', (_req, res) => {
res.sendFile(path.resolve('dist', 'index.html'))
})
httpServer.on('request', (req, res) => {
app(req, res)
})
httpServer.on('upgrade', (req, socket, head) => {
if (req.url?.startsWith('/wisp/')) {
wisp.routeRequest(req, socket as Socket, head)
} else {
socket.end()
}
})
httpServer.on('listening', () => {
consola.info(`Listening on http://localhost:${port}`)
})
httpServer.listen({
port
})