Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error c.res.status of 404 in middlewareHandler before next() #3788

Open
seepine opened this issue Dec 31, 2024 · 10 comments
Open

Error c.res.status of 404 in middlewareHandler before next() #3788

seepine opened this issue Dec 31, 2024 · 10 comments
Labels

Comments

@seepine
Copy link

seepine commented Dec 31, 2024

What version of Hono are you using?

4.6.14

What runtime/platform is your app running on? (with version if possible)

bun

What steps can reproduce the bug?

app.use(async (c, next) => {
    console.log('before', c.res.status);
    await next()
    console.log('after', c.res.status);
})
app.get('/test', ...)

What is the expected behavior?

Try curl

$ curl 127.0.0.1:3000/test
before 200    // It is possible to know whether the route exists.  
after 200

$ curl 127.0.0.1:3000/test2
before 404
after 404

What do you see instead?

Try curl

$ curl 127.0.0.1:3000/test
before 404
after 200

$ curl 127.0.0.1:3000/test2
before 404
after 404

Additional information

No response

@seepine seepine added the triage label Dec 31, 2024
@yusukebe
Copy link
Member

This is not a bug. c.res.status is 404 by default. The response code is determined when next() is called and a Response is returned.`

@yusukebe yusukebe added not bug and removed triage labels Dec 31, 2024
@seepine
Copy link
Author

seepine commented Dec 31, 2024

Thanks, but how to know the route exists? eg fastify support the is404 func on middleware.

fastify.addHook('onRequest', async function (request, reply) {
    if (request.is404) {
      // I can do something
    }
}

@MathurAditya724
Copy link
Contributor

You have app.notFound handler for this sort of scenario. You can see it here - https://hono.dev/docs/api/hono#not-found

@seepine
Copy link
Author

seepine commented Dec 31, 2024

@MathurAditya724 Sorry, this might not be my usage scenario of app.notFound.
Actually, I'm implementing a JWT middleware.
I need to judge to perform the verification only when the route exists, because if the verification fails, a 401 will be returned.

app.use(async (c, next) => {
    if (request.is404) {
      // not to verify jwt
      return next
    }
    // verify jwt 
    const token = c.req.header('authorization')
    const res = jwt.verify(token)
    if(!res.verified){
      return c.text("", 401)
    }
    await next()
})

@MathurAditya724
Copy link
Contributor

In the app (Hono instance), all the added routes can be viewed using .routes. You can query it to check if the route exists.

@seepine
Copy link
Author

seepine commented Dec 31, 2024

@MathurAditya724 Thanks, but Is there a way to get the original URL from the request? It cannot match req.path and route.path

// routes
[{
  path: "/user/:id",
  method: "ALL",
  handler: [AsyncFunction],
}]
// but c.req.routePath and c.req.path
// '/*'    '/user/1'

@MathurAditya724
Copy link
Contributor

You can get the URL for the request like this - c.req.url

@seepine
Copy link
Author

seepine commented Dec 31, 2024

Sorry, but it seem like c.req.path

console.log(c.req.method, c.req.routePath, c.req.path, c.req.url)
GET    /*   /user/1   http://127.0.0.1:3000/user/1

@MathurAditya724
Copy link
Contributor

I think we should take this discussion on Discord, that way more people can take a look at this and can help. And close this issue here.

@seepine
Copy link
Author

seepine commented Dec 31, 2024

Yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants