Fix Bun.serve port binding and error handling

- Add explicit hostname: '0.0.0.0' to bind all interfaces
- Add try/catch around server creation with proper error logging
- Add server error handler for runtime exceptions
- Add verification timeout to confirm server is actually listening
- Enhanced debugging to identify port binding issues

Fixes issue where server claimed success but wasn't actually listening on port 3000.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
riz 2025-11-20 01:14:27 +00:00
parent f6630579ba
commit 26f277893f
1 changed files with 32 additions and 7 deletions

View File

@ -81,9 +81,12 @@ export const createServer = async () => {
await ensureNotRunning();
console.log(`[DEBUG] Starting Bun.serve on port ${g.port}...`);
try {
g.server = Bun.serve({
port: g.port,
maxRequestBodySize: 1024 * 1024 * 128,
hostname: "0.0.0.0", // Explicitly bind to all interfaces
async fetch(req) {
const url = new URL(req.url) as URL;
url.pathname = url.pathname.replace(/\/+/g, "/");
@ -184,9 +187,31 @@ export const createServer = async () => {
return handle(req);
},
error(error) {
console.error(`[ERROR] Server error:`, error);
return new Response(`Internal Server Error: ${error.message}`, {
status: 500,
statusText: "Internal Server Error",
});
},
});
console.log(`[DEBUG] Server object created successfully!`);
console.log(`[DEBUG] Server actually listening on ${g.server.hostname}:${g.server.port}`);
// Verify the server is actually listening
setTimeout(() => {
if (g.server && g.server.port === g.port) {
console.log(`[DEBUG] ✓ Server verified to be listening on port ${g.port}`);
} else {
console.error(`[ERROR] ✗ Server not properly bound to port ${g.port}`);
}
}, 100);
} catch (error) {
console.error(`[ERROR] Failed to start Bun.serve:`, error);
throw error;
}
if (process.env.PRASI_MODE === "dev") {
g.log.info(`http://localhost:${g.server.port}`);