HonoH
Honoβ€’11mo ago
Mattèo

Hono + Bun + Better Auth 404 error when using app.route

Hey, I'm trying to setup a hono server like this, but when I call the /api/auth/ok endpoint I got a 404.
In fact I'm also using a app.route() in my entryfile. A very strange behavior break the "/api/auth/**" matching

Working version
const app = new Hono()

const userRoute = new Hono()
  // .get("/top", (c) => {
  //   return c.json({ message: "Top" }, 200)
  // })
  .get(
    "/:id",
    zValidator(
      "param",
      z.object({
        id: z.string(),
      })
    ),
    (c) => {
      return c.json({ message: `Hello ${c.req.param("id")}` }, 200)
    }
  )

const routes = app.route("/user", userRoute)

app.on(["POST", "GET"], "/api/auth/**", (c) => {
  return c.json({ message: "Hello" }, 200)
})

export default app

// curl http://localhost:3000/api/auth/ok 
// {"message":"Hello"}


Non working version
const userRoute = new Hono()
  .get("/top", (c) => {
    return c.json({ message: "Top" }, 200)
  }) // I've added this πŸ˜…πŸ˜…πŸ˜…πŸ˜…
  .get(
    "/:id",
    zValidator(
      "param",
      z.object({
        id: z.string(),
      })
    ),
    (c) => {
      return c.json({ message: `Hello ${c.req.param("id")}` }, 200)
    }
  )

const routes = app.route("/user", userRoute)

app.on(["POST", "GET"], "/api/auth/**", (c) => {
  return c.json({ message: "Hello" }, 200)
})

export default app
// curl http://localhost:3000/api/auth/ok 
// 404 Not Found


It's really strange that this bug happen when I have a new endpoint declarated

Has anyone had this problem before, or can anyone help me?

Wishing you a good day πŸ˜‰
Was this page helpful?