How to deploy a Bun HTTP server (Bun.serve)
Run a Bun.serve web server on Schokr: set hostname 0.0.0.0, use your allocated port, upload your code and start it with the Bun template.
Updated · 1 min read
To deploy a Bun web server on Schokr, create a server from the Bun template, upload your code, and call Bun.serve() with hostname: '0.0.0.0' and the port from your server's Network section.
1. Write the server
index.ts:
const port = Number(process.env.APP_PORT || 3000)
const server = Bun.serve({
hostname: '0.0.0.0',
port,
fetch(req) {
const url = new URL(req.url)
if (url.pathname === '/') return new Response('Hello from Bun')
if (url.pathname === '/health') return Response.json({ ok: true })
return new Response('Not found', { status: 404 })
},
error(err) {
console.error(err)
return new Response('Internal error', { status: 500 })
},
})
console.log(`Listening on ${server.hostname}:${server.port}`)
Bun runs TypeScript directly, so there is no build step.
2. Deploy
- In the console, create a server with the Bun template. 128 to 512 MB of memory suits most Bun servers.
- Note the port in the Network section.
- Upload
index.ts,package.jsonandbun.lock(if you use packages) with the file manager. Skipnode_modules. - Create a
.envfile withAPP_PORT=<your-port>. Bun loads.envautomatically. - In your server's Startup settings, set the main file to
index.ts. - Start the server and look for the "Listening on 0.0.0.0" line.
- Visit
http://<server-address>:<port>.
Routing with the routes option
Recent Bun versions (1.2.3 and later) support a routes object in Bun.serve:
Bun.serve({
hostname: '0.0.0.0',
port: Number(process.env.APP_PORT || 3000),
routes: {
'/': new Response('Home'),
'/api/users/:id': (req) => Response.json({ id: req.params.id }),
},
fetch: () => new Response('Not found', { status: 404 }),
})
If you get an error about routes, your Bun version is older; use the fetch handler instead. For a fuller router, see Hono on Bun.
Built-in extras
- SQLite:
bun:sqlitegives you a fast, file-based database with no dependencies. See SQLite in production. - WebSockets:
Bun.servehas native WebSocket support. See WebSockets on Schokr. - Static files: return
new Response(Bun.file('./public/logo.png')).
Troubleshooting
| Problem | Fix |
|---|---|
EADDRINUSE | Something else uses the port, or you started two servers. See port errors. |
| Runs, but browser times out | Check hostname is 0.0.0.0 and the port matches the Network section. |
| Package works in Node but not Bun | A few native Node addons are not supported; check the package's Bun compatibility. |
More details are in the Bun docs.