Open lord007tn opened 2 weeks ago
You can also wrote it like this to achieve what you want
// author.ts
import { Hono } from 'hono'
import { authMiddleware } from '@/middlewares/auth';
const app = new Hono().use(authMiddleware)
app.get('/', (c) => c.json('list authors'))
app.post('/', (c) => c.json('create an author', 201))
.get('/:id', (c) => c.json(`get ${c.req.param('id')}`))
export default app
Instead of doing app.use('/*', authMiddleware)
, you can do app.use(authMiddleware)
and place all the middleware before defining the routes
docs should state that middlewares should be used before the route declaration
Is this what you are looking for? - https://hono.dev/docs/guides/middleware#definition-of-middleware
@MathurAditya724 i read the whole docs no mention that middleware should be used before the route and for this it should be said somewhere
What version of Hono are you using?
4.5.10
What runtime/platform is your app running on?
Nodejs
What steps can reproduce the bug?
// authors.ts
// books.ts
// index.ts
so if you attempt to add authMiddleware to the child hono app it wont work
What is the expected behavior?
i think for better developer experience we should have the ability to add middlewares and encapsulate them inside children hono apps
What do you see instead?
middlewares only works if you add it to the root app and have it point to the absolute path
Additional information
No response