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

How to deploy a Hono app on Bun

Run a Hono web framework app on Schokr's Bun template, with the right hostname and port so it is reachable from the internet.

Updated · 1 min read

Hono runs on Bun by exporting an object with a fetch handler, but on Schokr you also need to set the hostname to 0.0.0.0 and the port to your allocated port. The most explicit way is to pass the Hono app to Bun.serve yourself.

1. Install Hono

bun add hono

This adds Hono to package.json. Upload that file and bun.lock with your code.

2. Write the app

src/index.ts:

import { Hono } from 'hono'
import { logger } from 'hono/logger'

const app = new Hono()

app.use(logger())

app.get('/', (c) => c.text('Hello from Hono'))
app.get('/api/users/:id', (c) => c.json({ id: c.req.param('id') }))
app.notFound((c) => c.json({ error: 'Not found' }, 404))

Bun.serve({
  hostname: '0.0.0.0',
  port: Number(process.env.APP_PORT || 3000),
  fetch: app.fetch,
})

console.log('Hono is running')

You may see Hono examples that use export default app instead. Bun picks that up automatically, but it uses Bun's default port and host unless you export port and hostname too:

export default {
  hostname: '0.0.0.0',
  port: Number(process.env.APP_PORT || 3000),
  fetch: app.fetch,
}

Use one approach, not both, or Bun will try to start two servers on the same port.

3. Deploy on Schokr

  1. Create a server with the Bun template in the console.
  2. Note your port in the Network section and write APP_PORT=<your-port> into a .env file in the server's root folder. Bun loads .env automatically.
  3. Upload src/, package.json and bun.lock with the file manager. Do not upload node_modules.
  4. Set the main file to src/index.ts in your server's Startup settings.
  5. Start the server and open http://<server-address>:<port>.

Useful middleware

NeedImport
CORSimport { cors } from 'hono/cors' (see CORS basics)
Static files on Bunimport { serveStatic } from 'hono/bun'
Basic authimport { basicAuth } from 'hono/basic-auth'

Example static files:

import { serveStatic } from 'hono/bun'
app.use('/static/*', serveStatic({ root: './' }))

Troubleshooting

  • EADDRINUSE right after start: you probably have both Bun.serve and export default with a fetch. Remove one. See port errors.
  • Cannot find package 'hono': package.json was not uploaded or install did not run. See module not found.

See the Hono docs for more.