Zum Inhalt springen

Netlify ​

Netlify bietet Hosting für statische Websites und Serverless-Backend-Dienste. Mit Edge Functions können wir dynamische Webseiten erstellen.

Edge Functions unterstützen Deno und TypeScript. Die Bereitstellung erfolgt einfach über die Netlify-CLI. Mit Hono kannst du Anwendungen für Netlify Edge Functions erstellen.

1. Einrichtung ​

Für Netlify steht eine Projektvorlage zur Verfügung. Starte dein Projekt mit dem Befehl „create-hono“. Wähle für dieses Beispiel die Vorlage netlify aus.

sh
npm create hono@latest my-app
sh
yarn create hono my-app
sh
pnpm create hono my-app
sh
bun create hono@latest my-app
sh
deno init --npm hono my-app

Wechsle nach my-app.

2. Hello World ​

Bearbeite netlify/edge-functions/index.ts:

ts
import { Hono } from 'jsr:@hono/hono'
import { handle } from 'jsr:@hono/netlify'

const app = new Hono()

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

export default handle(app)

3. Ausführen ​

Starte den Entwicklungsserver mit der Netlify-CLI. Öffne anschließend http://localhost:8888 in deinem Webbrowser.

sh
netlify dev

4. Bereitstellung ​

Du kannst mit dem Befehl netlify deploy bereitstellen.

sh
netlify deploy --prod

Context ​

Über c.env kannst du auf den Context von Netlify zugreifen:

ts
import { Hono } from 'jsr:@hono/hono'
import { handle } from 'jsr:@hono/netlify'

// Import the type definition
import type { Context } from 'https://edge.netlify.com/'

export type Env = {
  Bindings: {
    context: Context
  }
}

const app = new Hono<Env>()

app.get('/country', (c) =>
  c.json({
    'You are in': c.env.context.geo.country?.name,
  })
)

export default handle(app)

Veröffentlicht unter der MIT-Lizenz.