Why does Hono return 404 instead of 405 for unsupported HTTP methods? #4617
Replies: 4 comments 2 replies
|
@silvioprog you can use something like this |
|
@silvioprog I don't know if this is the best option, but it works for me. import { serve } from '@hono/node-server'
import { Hono } from 'hono'
const app = new Hono()
app.get("/", (c) => {
return c.text("GET Hello Hono!");
});
app.post('/hello', (c) => {
return c.text('POST Hello Hono!')
})
app.put('/hello', (c) => {
return c.text('PUT Hello Hono!')
})
app.all((c) => {
return c.text('Method not allowed', 405)
}).use("*",async (c) => {
return c.text('API not found', 404);
})
serve({
fetch: app.fetch,
port: 3000
}, (info) => {
console.log(`Server is running on http://localhost:${info.port}`)
})
I think it's a good approach on hono's part to return a 404 rather than a 405. For security reasons, I always prefer to return a 404 rather than saying that the method is not allowed. Maybe in the future there will be a method app.methodNotAllowed that will automatically return a 405. |
|
Thanks for the discussion! I've submitted a PR implementing PR: #4637 |
Solution: Return 405 Method Not Allowed in HonoHono returns 404 by default because it treats each method as a separate route. Here are ways to return 405: Option 1: Use
|





Uh oh!
There was an error while loading. Please reload this page.
Hono returns 404 when a route exists for a path but not for the requested HTTP method (e.g., POST sent to a GET route).
Is there a recommended way to return
405 Method Not Allowedinstead, or a built-in pattern/middleware to handle this?All reactions