new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Hono · all subjects

context

74 notes in this subject, read out of this brain and free to use. This is page 2 of 2.

EventContext type import for Cloudflare Pages

Import EventContext from 'hono/cloudflare-pages': import type { EventContext } from 'hono/cloudflare-pages'. Use it to type the Bindings in route handlers.

Access Cloudflare Workers bindings in Hono

Define a Bindings type with environment values, KV namespaces, R2 buckets, or Durable Objects, then pass it as a generic to Hono: `const app = new Hono<{ Bindings: Bindings }>()`. Access bindings in handlers via `c.env`.

Bindings type definition example

Define bindings type with properties like R2 buckets and strings: `type Bindings = { MY_BUCKET: R2Bucket; USERNAME: string; PASSWORD: string }`. Then pass to Hono generic and access in handlers like `await c.env.MY_BUCKET.put(key, c.req.body)`.

Auto-generate Cloudflare bindings types

Run `wrangler types --env-interface CloudflareBindings` to auto-generate a `worker-configuration.d.ts` file from your `wrangler.toml`. Use the `--env-interface` flag to avoid naming collisions with Hono's built-in Env type. Then import and pass this interface to Hono: `const app = new Hono<{ Bindings: CloudflareBindings }>()`

Use Cloudflare bindings in middleware

In Module Worker mode, access bindings in middleware by pulling them from context and passing to middleware functions. Example: in basicAuth middleware, pass `c.env.USERNAME` and `c.env.PASSWORD` as options: `const auth = basicAuth({ username: c.env.USERNAME, password: c.env.PASSWORD })`. This applies to all middleware needing environment variables.

Configure environment variables for Cloudflare Workers local development

Create a `.dev.vars` or `.env` file in the project root using dotenv syntax to set environment variables for local development. For example: `SECRET_KEY=value` and `API_TOKEN=eyJhbGc...`. Access these in handlers via `c.env.*`.

Access environment variables in Cloudflare Workers

Access environment variables via `c.env.*` in handlers. By default, `process.env` is not available in Cloudflare Workers. To use `process.env`, enable the `nodejs_compat_populate_process_env` compatibility flag, or import `env` from `cloudflare:workers`.

Set Cloudflare Workers environment variables before deployment

Before deploying to Cloudflare, set environment variables and secrets in the Cloudflare Workers project configuration via the Cloudflare dashboard, as documented in Cloudflare's environment variables guide.

Lambda@Edge Bindings with callback

When using Lambda@Edge callback, define the Bindings type to include: callback of type Callback from 'hono/lambda-edge', and request of type CloudFrontRequest from 'hono/lambda-edge'. Pass these bindings to Hono using type parameter: new Hono<{ Bindings: Bindings }>()

Netlify Context with geo data example

Example showing how to access Netlify's Context in a Hono app on Netlify Edge Functions. Define Env type with Bindings containing context: Context imported from 'https://edge.netlify.com/'. Create a Hono app typed with <Env>. Use c.env.context.geo.country?.name to access the user's country information and return it as JSON.

Access raw Node.js APIs from context

Access raw Node.js request and response objects through c.env.incoming and c.env.outgoing in handlers. Use HttpBindings or Http2Bindings type from '@hono/node-server' to type the context bindings.

Raw Node.js API access example

Example of accessing raw Node.js APIs in Hono on Node.js: import { Hono } from 'hono' import { serve, type HttpBindings } from '@hono/node-server' type Bindings = HttpBindings & { /* ... */ } const app = new Hono<{ Bindings: Bindings }>() app.get('/', (c) => { return c.json({ remoteAddress: c.env.incoming.socket.remoteAddress, }) }) serve(app)

Testing c.env with app.request

To set c.env for testing, pass an environment object as the third parameter to app.request. This is useful for mocking Cloudflare Workers Bindings and other environment variables. Example: const MOCK_ENV = { API_HOST: 'example.com', DB: { prepare: () => {} } }; const res = await app.request('/posts', {}, MOCK_ENV);

Context object in Hono

Hono provides a Context object that enables easy access to Request/Response.

Give your agent this brain