Skip to content

Node.js

Node.js is an open-source, cross-platform JavaScript runtime environment.

Hono was not designed for Node.js at first. But with a Node.js Adapter it can run on Node.js as well.

INFO

It works on Node.js versions greater than 18.x. The specific required Node.js versions are as follows:

  • 18.x => 18.14.1+
  • 19.x => 19.7.0+
  • 20.x => 20.0.0+

Essentially, you can simply use the latest version of each major release.

1. Setup

A starter for Node.js is available. Start your project with "create-hono" command. Select nodejs template for this example.

sh
npm create hono@latest my-app
sh
yarn create hono my-app
sh
pnpm create hono my-app
sh
bunx create-hono my-app
sh
deno run -A npm:create-hono my-app

Move to my-app and install the dependencies.

sh
cd my-app
npm i
sh
cd my-app
yarn
sh
cd my-app
pnpm i
sh
cd my-app
bun i

2. Hello World

Edit src/index.ts:

ts
import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const app = new Hono()
app.get('/', (c) => c.text('Hello Node.js!'))

serve(app)

3. Run

Run the development server locally. Then, access http://localhost:3000 in your Web browser.

sh
npm run dev
sh
yarn dev
sh
pnpm dev

Change port number

You can specify the port number with the port option.

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

Access the raw Node.js APIs

You can access the Node.js APIs from c.env.incoming and c.env.outgoing.

ts
import { Hono } from 'hono'
import { serve, type HttpBindings } from '@hono/node-server'
// or `Http2Bindings` if you use HTTP2

type Bindings = HttpBindings & {/* ... */}

const app = new Hono<{ Bindings: Bindings }>()

app.get('/', (c) => {
  return c.json({
    remoteAddress: c.env.incoming.socket.remoteAddress,
  })
})

serve(app)

Serve static files

You can use serveStatic to serve static files from the local file system.

ts
import { serveStatic } from '@hono/node-server/serve-static'

app.use('/static/*', serveStatic({ root: './' }))

rewriteRequestPath

If you want to map http://localhost:3000/static/* to ./statics, you can use the rewriteRequestPath option:

ts
app.get(
  '/static/*',
  serveStatic({
    root: './',
    rewriteRequestPath: (path) => path.replace(/^\/static/, '/statics'),
  })
)

http2

You can run hono on a Node.js http2 Server.

unencrypted http2

ts
import { createServer } from 'node:http2'

const server = serve({
  fetch: app.fetch
  createServer,
})

encrypted http2

ts
import { createSecureServer } from 'node:http2'
import { readFileSync } from 'node:fs'

const server = serve({
  fetch: app.fetch,
  createServer: createSecureServer,
  serverOptions: {
    key: readFileSync('localhost-privkey.pem'),
    cert: readFileSync('localhost-cert.pem'),
  },
})

Dockerfile

Here is an example of a Dockerfile.

Dockerfile
FROM node:20-alpine AS base

FROM base AS builder

RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package*json tsconfig.json src ./

RUN npm ci && \
    npm run build && \
    npm prune --production

FROM base AS runner
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 hono

COPY --from=builder --chown=hono:nodejs /app/node_modules /app/node_modules
COPY --from=builder --chown=hono:nodejs /app/dist /app/dist
COPY --from=builder --chown=hono:nodejs /app/package.json /app/package.json

USER hono
EXPOSE 3000

CMD ["node", "/app/dist/index.js"]

The following steps shall be taken in advance.

  1. Add "outDir": "./dist" to the compilerOptions section tsconfig.json.
  2. Add "exclude": ["node_modules"] to tsconfig.json.
  3. Add "build": "tsc" to script section of package.json.
  4. Run npm install typescript --save-dev.
  5. Add "type": "module" to package.json.

Released under the MIT License.