-
-
Notifications
You must be signed in to change notification settings - Fork 488
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
How do you create middleware for a specific route and method? #207
Comments
We were just talking about it! #189 If this comes true, we can write this way. const route = app.route('/users')
route.post('/', validate(userValidation.createUser), userController.createUser) We are planning to release the Hono including this feature. Maybe it's v1.2.0. Before it is released, you can write like this: const mid = async (c: Context, next) => {
await next()
c.header('x-custom', 'abc')
}
app.use('*', async (c, next) => {
if (c.req.method === 'GET') {
await mid(c, next)
} else {
await next()
}
})
app.get('/', (c) => {
return c.text('Hello')
}) Thank you! |
Oh haha that's awesome that it's in progress. I hope the 1.2 release is
coming soon! I'm building an Api using Hono and the new features will be
super helpful!
…On Mon, 9 May 2022, 14:40 Yusuke Wada, ***@***.***> wrote:
Hi @OultimoCoder <https://github.com/OultimoCoder>
We were just talking about it! #189
<#189>
If this comes true, we can write this way.
const route = app.route('/users')route.post('/', validate(userValidation.createUser), userController.createUser)
We are planning to release the Hono including this feature. Maybe it's
v1.2.0.
Before it is released, you can write like this:
const mid = async (c: Context, next) => {
await next()
c.header('x-custom', 'abc')}
app.use('*', async (c, next) => {
if (c.req.method === 'GET') {
await mid(c, next)
} else {
await next()
}})
app.get('/', (c) => {
return c.text('Hello')})
Thank you!
—
Reply to this email directly, view it on GitHub
<#207 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADSHYXLPT53CSM4JYI4TBULVJEIUHANCNFSM5VN7VAMA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
We released v1.2.0 including new features. You can write like this: import { Hono, Route } from 'hono'
const app = new Hono()
const route = new Route()
route.post('/', validate(userValidation.createUser), userController.createUser)
app.route('/users', route) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Say I wanted to validate the parameters for a post request using middleware but not affect the other methods like get etc, I would like to be able to do:
But that isn't possible, the only way I can see to do it currently is:
But this does it for all methods in the
/users
route, I could check in the middleware for the route method but this isn't ideal. Is there a way to do this that I am missing?The text was updated successfully, but these errors were encountered: