Skip to content

Bun Hono Port

Directory Structure

.
`-- hono
    |-- .env
    |-- package.json
    |-- src
    |   `-- index.ts
    `-- tsconfig.json

3001port

sh
HONO_PORT=3001
ts
import { serve } from 'bun'
import { Hono } from 'hono'

const app = new Hono()

const defaultPort = 3001
const port = process.env.HONO_PORT !== undefined ? parseInt(process.env.HONO_PORT) : defaultPort
console.log(process.env.HONO_PORT)

app.get('/', (c) => {
  return c.text('Hello Hono!')
})

serve({ fetch: app.fetch, port: port })

export default serve

if (process.pid) {
  console.log('This process is server pid ' + process.pid)
}

Execution

sh
cd hono
sh
bun run dev

Accesse Browser

http://localhost:3001/

Response

Hello Hono!

WARNING

.envを実行するディレクトリに置かないと読み込まれないので注意が必要

hono/の階層で実行

.envが読み込まれる。

sh
cd hono
sh
bun run dev
sh
3001
This process is server pid *****

hono/以外でで実行

.envが読み込まれない。

sh
cd hono
sh
bun run dev
sh
undefined
This process is server pid *****