question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Processed handler should be only one

See original GitHub issue

In the Iusse #189 , we decided to treat handlers and middleware as the same. We can write like these:

app.post('/a', basicAuth(), bodyParse(), (c) => c.text('a)')

This is good.

How about Handler + Handler pattern.

app.get('/a', (c) => {
  console.log('a')
  return c.text('a')
})

app.get('/:slug', (c) => {
  console.log('slug')
  return c.text('slug')
})

This Response body will be a. This is expected.

BUT, in #258 I notice that every handler is processed. console.log emits slug, a.

The reason:

  • All routes including middleware are treated as the same.
  • A path will match multiple routes = handlers or middleware.
  • If multiple handlers are matched, every handler will be invoked.
  • Route returning Response object is determined as the handler.
  • We don’t know whether the route is a handler or Middleware until dispatching 😕

I think this design is not so good. Only one handler should be invoked. In this case, console.log should emit only a. So, we have to:

  • Determine handler or middleware.
  • Make only one handler invoked.

How can we do that?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:32 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
dan-leecommented, May 26, 2022

Hey @yusukebe, thanks a lot for your great work! It works perfectly now 🤩

1reaction
yusukebecommented, May 26, 2022

Hi @dan-lee !

We’ve released v1.4.1. https://github.com/honojs/hono/releases/tag/v1.4.1

This release includes the feature that you expected. Update routing priority and fixed the issue about GraghQL server middleware. The following code works well. Please check it out!

import { buildSchema } from 'graphql'
import { Hono } from 'hono'
import { graphqlServer } from 'hono/graphql-server'

const app = new Hono()

const schema = buildSchema(`
type Query {
  hello: String
}
`)

const rootValue = {
  hello: () => 'Hello Hono!',
}

app.use(
  '/graphql',
  graphqlServer({
    schema,
    rootValue,
  })
)

app.get('*', (c) => {
  return c.text('fallback')
})

export default app
Read more comments on GitHub >

github_iconTop Results From Across the Web

Signal handler being called once per process - Stack Overflow
The problem is that cleanup is being called multiple times (once per each running process). How can I make it so cleanup is...
Read more >
The DLQ handler rules table - IBM
The DLQ handler rules table defines how the DLQ handler is to process messages that arrive on the DLQ. ... You can specify...
Read more >
Chain of Responsibility - Refactoring.Guru
If it can, it doesn't pass the request any further. So it's either only one handler that processes the request or none at...
Read more >
Handler - Android Developers
A Handler allows you to send and process Message and Runnable objects ... This method is only for use in very special circumstances...
Read more >
HandlerAdapters in Spring MVC - Baeldung
The DispatcherServlet then uses a HandlerAdapter to invoke this method. ... If a web application uses only controllers then we don't need to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found