Bun.js in Browser Demo
Client ID: Not connected
Client URL: Not available
({ guestBook: [], fetch(req) { const url = new URL(req.url); if (url.pathname === "/") { return new Response(this.renderGuestBook(), { headers: { "Content-Type": "text/html" } }); } if (url.pathname === "/sign" && req.method === "POST") { return this.handleSignGuestBook(req); } if (url.pathname === "/api") { return Response.json({ message: "This is a JSON response from Bun.js", clientId: window.clientId, clientUrl: window.clientUrl }); } return new Response("Not Found", { status: 404 }); }, renderGuestBook() { let html = `
Welcome to our Guest Book!
Sign Guest Book
Entries:
${this.guestBook.map(entry => `
${entry.name}
: ${entry.message}
`).join('')}
`; return html; }, async handleSignGuestBook(req) { const formData = await req.formData(); const name = formData.get("name"); const message = formData.get("message"); this.guestBook.push({ name, message }); return new Response("Entry added successfully", { status: 302, headers: { "Location": "/" } }); } })
Run Code