honojs / hono

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

getConnInfo Cannot destructure property 'remoteAddr' of 'c.env' as it is undefined. #3043

Open winston0410 opened 1 week ago

winston0410 commented 1 week ago

What version of Hono are you using?

4.4.8

What runtime/platform is your app running on?

Deno

What steps can reproduce the bug?

import { Hono } from "jsr:@hono/hono@^4.4.8";
import { getPath } from "jsr:@hono/hono@^4.4.8/utils/url";
import { getConnInfo } from 'jsr:@hono/hono@^4.4.8/deno'

const app = new Hono();
app.use(async (c, next) => {
  const { method } = c.req;
  const path = getPath(c.req.raw);
  const connInfo = getConnInfo(c)

  console.log('check info', connInfo)

  await next();
});

app.get("/health/readiness", (c) => {
  return c.json({
    status: "ok",
  });
});

export default app;

What is the expected behavior?

Running without error

What do you see instead?

TypeError: Cannot destructure property 'remoteAddr' of 'c.env' as it is undefined. at getConnInfo (https://jsr.io/@hono/hono/4.4.8/src/adapter/deno/conninfo.ts:9:11) at dispatch (https://jsr.io/@hono/hono/4.4.8/src/compose.ts:74:23) at https://jsr.io/@hono/hono/4.4.8/src/compose.ts:40:12 at https://jsr.io/@hono/hono/4.4.8/src/hono-base.ts:443:31 at Hono.dispatch (https://jsr.io/@hono/hono/4.4.8/src/hono-base.ts:454:6) at Hono.fetch (https://jsr.io/@hono/hono/4.4.8/src/hono-base.ts:473:17) at handler (ext:deno_http/00_serve.ts:557:26) at ext:deno_http/00_serve.ts:332:24 at ext:deno_http/00_serve.ts:503:29

Additional information

No response

EdamAme-x commented 1 week ago

hi @winston0410 Try using Deno.serve.

yusukebe commented 1 week ago

Hi @winston0410

As @EdamAme-x said, please use Deno.serve() instead of export default app:

Deno.serve(app.fetch)

The starter template and the website was using export default app, but as you faced it, it might be better to use Deno.serve(). So, I've updated the docs:

https://github.com/honojs/website/pull/420

Thanks.