Open History to see the last commit
1import { toNodeHandler } from 'srvx/node'
2import path from 'node:path'
3import express from 'express'
4import { createProxyMiddleware } from 'http-proxy-middleware'
5β
6const port = process.env.PORT || 3000
7β
8const startPort = process.env.START_PORT || 3001
9β
10export async function createStartServer() {
11 const server = (await import('./dist/server/server.js')).default
12 const nodeHandler = toNodeHandler(server.fetch)
13β
14 const app = express()
15β
16 app.use(express.static('./dist/client'))
17β
18 app.use(async (req, res, next) => {
19 try {
20 await nodeHandler(req, res)
21 } catch (error) {
22 next(error)
23 }
24 })
25β
26 return { app }
27}
28β
29export async function createSpaServer() {
30 const app = express()
31β
32 app.use(
33 '/api',
34 createProxyMiddleware({
35 target: `http://localhost:${startPort}/api`, // Replace with your target server's URL
36 changeOrigin: false, // Needed for virtual hosted sites,
37 }),
38 )
39β
40 app.use(
41 '/_serverFn',
42 createProxyMiddleware({
43 target: `http://localhost:${startPort}/_serverFn`, // Replace with your target server's URL
44 changeOrigin: false, // Needed for virtual hosted sites,
45 }),
46 )
47β
48 app.use(express.static('./dist/client'))
49β
50 app.get('/{*splat}', (req, res) => {
51 res.sendFile(path.resolve('./dist/client/index.html'))
52 })
53β
54 return { app }
55}
56β
57createSpaServer().then(async ({ app }) =>
58 app.listen(port, () => {
59 console.info(`Client Server: http://localhost:${port}`)
60 }),
61)
62β
63createStartServer().then(async ({ app }) =>
64 app.listen(startPort, () => {
65 console.info(`Start Server: http://localhost:${startPort}`)
66 }),
67)