NewSchokr 13 is here. A whole new UI, seven locations, snapshots and more. Read more →

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

  1. In the console, create a server with the Bun template. 128 to 512 MB of memory suits most Bun servers.
  2. Note the port in the Network section.
  3. Upload index.ts, package.json and bun.lock (if you use packages) with the file manager. Skip node_modules.
  4. Create a .env file with APP_PORT=<your-port>. Bun loads .env automatically.
  5. In your server's Startup settings, set the main file to index.ts.
  6. Start the server and look for the "Listening on 0.0.0.0" line.
  7. 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:sqlite gives you a fast, file-based database with no dependencies. See SQLite in production.
  • WebSockets: Bun.serve has native WebSocket support. See WebSockets on Schokr.
  • Static files: return new Response(Bun.file('./public/logo.png')).

Troubleshooting

ProblemFix
EADDRINUSESomething else uses the port, or you started two servers. See port errors.
Runs, but browser times outCheck hostname is 0.0.0.0 and the port matches the Network section.
Package works in Node but not BunA few native Node addons are not supported; check the package's Bun compatibility.

More details are in the Bun docs.