Files
gallery/backend/server.js
T
2026-06-03 23:09:21 -06:00

50 lines
1.5 KiB
JavaScript

const PUBLIC_DIR = './public'
const PORT = process.env.PORT ? Number(process.env.PORT) : 3000
function getContentType(path) {
if (path.endsWith('.html')) return 'text/html'
if (path.endsWith('.js')) return 'application/javascript'
if (path.endsWith('.css')) return 'text/css'
if (path.endsWith('.json')) return 'application/json'
if (path.endsWith('.png')) return 'image/png'
if (path.endsWith('.jpg') || path.endsWith('.jpeg')) return 'image/jpeg'
if (path.endsWith('.svg')) return 'image/svg+xml'
if (path.endsWith('.woff2')) return 'font/woff2'
if (path.endsWith('.woff')) return 'font/woff'
if (path.endsWith('.ttf')) return 'font/ttf'
return 'application/octet-stream'
}
Bun.serve({
port: PORT,
async fetch(req) {
const url = new URL(req.url)
if (url.pathname === '/health') {
return new Response('OK', { status: 200 })
}
const filePath = url.pathname === '/' ? '/index.html' : url.pathname
const fullPath = `${PUBLIC_DIR}${filePath}`
const file = Bun.file(fullPath)
if (await file.exists()) {
return new Response(file, {
headers: { 'Content-Type': getContentType(filePath) },
})
}
// SPA fallback
const indexFile = Bun.file(`${PUBLIC_DIR}/index.html`)
if (await indexFile.exists()) {
return new Response(indexFile, {
headers: { 'Content-Type': 'text/html' },
})
}
return new Response('Not Found', { status: 404 })
},
})
console.log(`Server running on http://localhost:${PORT}`)