-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (52 loc) · 1.57 KB
/
server.js
File metadata and controls
67 lines (52 loc) · 1.57 KB
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
import { toNodeHandler } from 'srvx/node'
import path from 'node:path'
import express from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'
const port = process.env.PORT || 3000
const startPort = process.env.START_PORT || 3001
export async function createStartServer() {
const server = (await import('./dist/server/server.js')).default
const nodeHandler = toNodeHandler(server.fetch)
const app = express()
app.use(express.static('./dist/client'))
app.use(async (req, res, next) => {
try {
await nodeHandler(req, res)
} catch (error) {
next(error)
}
})
return { app }
}
export async function createSpaServer() {
const app = express()
app.use(
'/api',
createProxyMiddleware({
target: `http://localhost:${startPort}/api`, // Replace with your target server's URL
changeOrigin: false, // Needed for virtual hosted sites,
}),
)
app.use(
'/_serverFn',
createProxyMiddleware({
target: `http://localhost:${startPort}/_serverFn`, // Replace with your target server's URL
changeOrigin: false, // Needed for virtual hosted sites,
}),
)
app.use(express.static('./dist/client'))
app.get('/{*splat}', (req, res) => {
res.sendFile(path.resolve('./dist/client/index.html'))
})
return { app }
}
createSpaServer().then(async ({ app }) =>
app.listen(port, () => {
console.info(`Client Server: http://localhost:${port}`)
}),
)
createStartServer().then(async ({ app }) =>
app.listen(startPort, () => {
console.info(`Start Server: http://localhost:${startPort}`)
}),
)