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.
npm create hono@latest my-appyarn create hono my-apppnpm create hono my-appbun create hono@latest my-appdeno init --npm hono my-appWechsle nach my-app.
2. Hello World
Bearbeite netlify/edge-functions/index.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.
netlify dev4. Bereitstellung
Du kannst mit dem Befehl netlify deploy bereitstellen.
netlify deploy --prodContext
Über c.env kannst du auf den Context von Netlify zugreifen:
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)