Closed yusukebe closed 1 year ago
why @yusukebe didn't mention the pull request?
@codenoid
Which pull request? This topic is already documented: https://hono.dev/getting-started/cloudflare-workers#using-variables-in-middleware
@yusukebe I want to read env var outside the handler, is it possible?
On Cloudflare Workers, If you use a Module Worker mode, you can't read the variables outside the handler.
So I have to create instance at request each time?
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.
@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) })
Hi @Sammy3102
Write them on wrangler.toml: https://developers.cloudflare.com/workers/configuration/environment-variables/
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
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.
Write a document on how to handle Environment variables for Cloudflare Workers. Like this: