Files
aiworker-frontend/server.ts
Hector Ros 137f1fe110
All checks were successful
Build and Push Frontend / build (push) Successful in 4s
Fix HTML serving: use Bun.file() instead of HTMLBundle import
Fixes [object HTMLBundle] error by properly reading HTML file

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
2026-01-20 01:30:24 +01:00

64 lines
1.7 KiB
TypeScript

const PORT = process.env.PORT || 3001
const BACKEND_URL = process.env.BACKEND_URL || 'http://localhost:3000'
Bun.serve({
port: PORT,
async fetch(req) {
const url = new URL(req.url)
// API proxy to backend
if (url.pathname.startsWith('/api/')) {
const backendUrl = `${BACKEND_URL}${url.pathname}${url.search}`
const headers = new Headers(req.headers)
// Forward cookies
const cookie = req.headers.get('cookie')
if (cookie) {
headers.set('cookie', cookie)
}
const backendReq = new Request(backendUrl, {
method: req.method,
headers,
body: req.method !== 'GET' && req.method !== 'HEAD' ? await req.text() : undefined,
})
const backendRes = await fetch(backendReq)
// Forward Set-Cookie headers
const setCookies = backendRes.headers.get('set-cookie')
const responseHeaders = new Headers(backendRes.headers)
if (setCookies) {
responseHeaders.set('set-cookie', setCookies)
}
return new Response(backendRes.body, {
status: backendRes.status,
statusText: backendRes.statusText,
headers: responseHeaders,
})
}
// Serve static files from public directory
const staticFile = Bun.file(`./public${url.pathname}`)
if (await staticFile.exists()) {
return new Response(staticFile)
}
// For all other routes (SPA routing), serve index.html
const indexFile = Bun.file('./public/index.html')
return new Response(indexFile, {
headers: {
'Content-Type': 'text/html',
},
})
},
development: {
hmr: true,
},
})
console.log(`Frontend server running on http://localhost:${PORT}`)
console.log(`Proxying API requests to ${BACKEND_URL}`)