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}`)