honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
20.43k stars 587 forks source link

Document for `Env` variables #194

Closed yusukebe closed 1 year ago

yusukebe commented 2 years ago

Write a document on how to handle Environment variables for Cloudflare Workers. Like this:

app.post('/posts', async (c, next) => {
  const auth = basicAuth({ username: c.env.NAME, password: c.env.PASS })
  await auth(c, next)
})
codenoid commented 1 year ago

why @yusukebe didn't mention the pull request?

yusukebe commented 1 year ago

@codenoid

Which pull request? This topic is already documented: https://hono.dev/getting-started/cloudflare-workers#using-variables-in-middleware

codenoid commented 1 year ago

@yusukebe I want to read env var outside the handler, is it possible?

yusukebe commented 1 year ago

On Cloudflare Workers, If you use a Module Worker mode, you can't read the variables outside the handler.

fujohnwang commented 10 months ago

So I have to create instance at request each time?

yusukebe commented 10 months ago

If you are using Cloudflare Workers, I recommend creating an instance every time. The process may run for a while, but it could disappear at any time.

Sammy3102 commented 8 months ago

@yusukebe how do you create env variables in hono for cloudflare examples , like do we create a .env file?? The below code is only about how to access the env variable but not about how to create them app.post('/posts', async (c, next) => { const auth = basicAuth({ username: c.env.NAME, password: c.env.PASS }) await auth(c, next) })

yusukebe commented 8 months ago

Hi @Sammy3102

Write them on wrangler.toml: https://developers.cloudflare.com/workers/configuration/environment-variables/

Sammy3102 commented 8 months ago

Hi @Sammy3102

Write them on wrangler.toml: https://developers.cloudflare.com/workers/configuration/environment-variables/

Hey thanks alot I didn't know toml syntax & all this time I had #[vars] so I couldn't access any of the variables because it was commented I would say that this topic on "how to create env variables" should be there in hono in cloudfare workers section too

NaingAungLuu commented 5 months ago

Hello guys. I had the same issue while trying to access the environment variables that were stored in .dev.vars. My setup was using workerd runtime and took me a long time to figure out.

The problem with my setup is that I was starting my Hono app with the app.fire() as follows:

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

After updating my code to start the app with export default app;, the cloudflare workers start to recognize the environment variables.

const app = new Hono<{ Bindings: Bindings}>();
// Can't access env var
app.fire()

// Can access env var
export default app;

But now, we recommend using Module Worker mode because such as that the binding variables are localized.

The particular information was mentioned in Documentation, in Cloudflare Workers page, but wasn't easily noticeable in the docs.