Hono
Documentation
Quickstart
Installation
npm create --yes hono@latest my-app# For the `Which template do you want to use?` prompt, select `nodejs`cd my-appnpm uninstall @hono/node-servernpm install bunny-hononpm install --save-dev esbuild esbuild-plugin-node-protocol-imports
Configuration
Update the package.json
file to add a build script:
{ "name": "my-app", "type": "module", "scripts": { "dev": "tsx watch src/index.ts", "build": "tsx src/esbuild" }, "dependencies": { "bunny-hono": "^0.0.4", "hono": "^4.6.6" }, "devDependencies": { "@types/node": "^20.11.17", "esbuild": "^0.24.0", "esbuild-plugin-node-protocol-imports": "^0.0.1", "tsx": "^4.7.1" }}
Update the template to export the Hono app and remove @hono/node-server
code:
src/index.ts
import { serve } from '@hono/node-server'import { Hono } from 'hono'import type { BunnyEnv } from 'bunny-hono'const app = new Hono<BunnyEnv>()app.get('/', (c) => { return c.text('Hello Hono!')})const port = 3000console.log(`Server is running on port ${port}`)serve({ fetch: app.fetch, port,})export default app
Create a standalone server handler:
src/handler.ts
import { standaloneHandler } from 'bunny-hono'import app from './index.js'standaloneHandler(app)
Or, create a middleware server handler:
src/handler.ts
import { middlewareHandler } from 'bunny-hono'import app from './index.js'middlewareHandler(app)
Create a build script:
src/esbuild.ts
import fs from 'node:fs/promises'import { builtinModules } from 'node:module'import { build } from 'esbuild'import esbuildPluginNodeProtocolImports from 'esbuild-plugin-node-protocol-imports'const MINIFY = process.env.MINIFY === 'false' ? false : trueconst allBuiltinModules = [ ...builtinModules, ...builtinModules.map((builtinModule) => `node:${builtinModule}`),]await fs.rm('dist', { recursive: true, force: true,})await build({ banner: { js: 'import * as process from "node:process";import { Buffer } from "node:buffer";globalThis.process ??= process;globalThis.Buffer ??= Buffer;globalThis.global ??= globalThis;', }, bundle: true, define: { 'process.env.NODE_ENV': '"production"' }, entryPoints: ['src/handler.ts'], external: allBuiltinModules, format: 'esm', keepNames: !MINIFY, minify: MINIFY, outdir: 'dist', packages: 'bundle', platform: 'node', sourcemap: !MINIFY, target: 'node20.17.0', plugins: [esbuildPluginNodeProtocolImports],})
For more information on bundling for edge scripts, please read the guide.
If you would like to render with React, you can use the @hono/react-renderer
. However, client-side hydration and interactivity is only available with the HonoX adapter.
Configuration with Static Site Generation (SSG)
This adapter was made specifically for server-side rendering (SSR). For SSG, follow these instructions instead.
Deployment
Build
npm run build
Deploy
npx --yes bunny-launcher@latest --interactive