Skip to content

Deno

Deno is a JavaScript runtime built on V8. It's not Node.js. Hono also works on Deno.

You can use Hono, write the code with TypeScript, run the application with the deno command, and deploy it to "Deno Deploy".

1. Install Deno

First, install deno command. Please refer to the official document.

2. Setup

A starter for Deno is available. Start your project with "create-hono" command.

sh
deno run -A npm:create-hono my-app

Select deno template for this example.

Move into my-app. For Deno, you don't have to install Hono explicitly.

sh
cd my-app

3. Hello World

Write your first application.

ts
import { Hono } from 'hono'

const app = new Hono()

app.get('/', (c) => c.text('Hello Deno!'))

export default app

4. Run

Just this command:

sh
deno task start

Change port number

You can specify the port number by updating the `deno.json``:

json
{
  "tasks": {
    "start": "deno serve main.ts"
    "start": "deno serve --port 8787 main.ts"
  }
}

Serve static files

To server static files, use serveStatic imported from hono/middleware.ts.

ts
import { Hono } from 'hono'
import { serveStatic } from 'hono/deno'

const app = new Hono()

app.use('/static/*', serveStatic({ root: './' }))
app.use('/favicon.ico', serveStatic({ path: './favicon.ico' }))
app.get('/', (c) => c.text('You can access: /static/hello.txt'))
app.get('*', serveStatic({ path: './static/fallback.txt' }))

export default app

For the above code, it will work well with the following directory structure.

./
├── favicon.ico
├── index.ts
└── static
    ├── demo
    │   └── index.html
    ├── fallback.txt
    ├── hello.txt
    └── images
        └── dinotocat.png

rewriteRequestPath

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

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

mimes

You can add MIME types with mimes:

ts
app.get(
  '/static/*',
  serveStatic({
    mimes: {
      m3u8: 'application/vnd.apple.mpegurl',
      ts: 'video/mp2t',
    },
  })
)

onNotFound

You can specify handling when the requested file is not found with onNotFound:

ts
app.get(
  '/static/*',
  serveStatic({
    onNotFound: (path, c) => {
      console.log(`${path} is not found, you access ${c.req.path}`)
    },
  })
)

Deno Deploy

Deno Deploy is an edge runtime platform for Deno. We can publish the application world widely on Deno Deploy.

Hono also supports Deno Deploy. Please refer to the official document.

Testing

Testing the application on Deno is easy. You can write with Deno.test and use assert or assertEquals from @std/assert.

sh
deno add @std/assert
ts
import { Hono } from 'hono'
import { assertEquals } from '@std/assert'

Deno.test('Hello World', async () => {
  const app = new Hono()
  app.get('/', (c) => c.text('Please test me'))

  const res = await app.request('http://localhost/')
  assertEquals(res.status, 200)
})

Then run the command:

sh
deno test hello.ts

npm: specifier

npm:hono is also available. You can use it by fixing the deno.json:

json
{
  "imports": {
    "hono": "jsr:@hono/hono"
    "hono": "npm:hono"
  }
}

You can use either npm:hono or jsr:@hono/hono.

If you want to use Third-party Middleware such as npm:@hono/zod-validator with the TypeScript Type inferences, you need to use the npm: specifier.

json
{
  "imports": {
    "hono": "npm:hono",
    "zod": "npm:zod",
    "@hono/zod-validator": "npm:@hono/zod-validator"
  }
}

Released under the MIT License.