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
- Create a server with the Bun template in the console.
- Note your port in the Network section and write
APP_PORT=<your-port>into a.envfile in the server's root folder. Bun loads.envautomatically. - Upload
src/,package.jsonandbun.lockwith the file manager. Do not uploadnode_modules. - Set the main file to
src/index.tsin your server's Startup settings. - Start the server and open
http://<server-address>:<port>.
Useful middleware
| Need | Import |
|---|---|
| CORS | import { cors } from 'hono/cors' (see CORS basics) |
| Static files on Bun | import { serveStatic } from 'hono/bun' |
| Basic auth | import { basicAuth } from 'hono/basic-auth' |
Example static files:
import { serveStatic } from 'hono/bun'
app.use('/static/*', serveStatic({ root: './' }))
Troubleshooting
EADDRINUSEright after start: you probably have bothBun.serveandexport defaultwith afetch. Remove one. See port errors.Cannot find package 'hono':package.jsonwas not uploaded or install did not run. See module not found.
See the Hono docs for more.