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

feat: app.route(path, Route) #203

Merged
merged 1 commit into from
May 7, 2022
Merged

feat: app.route(path, Route) #203

merged 1 commit into from
May 7, 2022

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented May 6, 2022

Using Route object with app.route method, we can make the large structured applications more easily.

For example:

// route.ts
import { Route } from 'hono'
import { cors } from 'hono/cors'
import { poweredBy } from 'hono/powered-by'

export const route = new Route()

route
  .get('/', (c) => c.text('List posts'))
  .post(cors(), poweredBy(), (c) => {
    return c.text('Create post')
  })
  .get('/:id{[0-9]+}', (c) => {
    const id = c.req.param('id')
    return c.text(`Your id is ${id}`)
  })
// index.ts
import { route } from './route'

import { Hono } from 'hono'
const app = new Hono()

app.route('/v1', route)

app.fire()

Related #200
Close #201

@metrue
Copy link
Contributor

metrue commented May 6, 2022

Hi @yusukebe

I know the idea of router.route is trying to make routes management easier for large structure, but I think I may need a little more time to think about it, mainly about two things,

  • the interface of hono (the app object)
  • the interface of router

but please make your call to merge it as you want.

@yusukebe
Copy link
Member Author

yusukebe commented May 6, 2022

@metrue OK! Please think about it.

@metrue
Copy link
Contributor

metrue commented May 6, 2022

hono is like a connector, it gets the FetchEvent and dispatches to the handle function, return the Response in the end; To make thing scalable, hono introduces the context to manage the request and response, which make it easy to support middlewares. Hono is a Cloudflare Worker (and Edge) connector with expressive middleware system.

So maybe to keep its simplicity,

  • hono responses for the FetchEvent dispatch (_.dispatch), and middlewares invoke (_.use)
  • router is just another middleware, which route a request (with request path and http method) to a (a group) of handle functions (or middlewares)

Then comes to a project which has a bunch of APIs, we can manage it like,

const usersRouter = new Router('/users')
const postsRouter = new Router('/posts')

app.use(usersRouter.routes(),  postsRouter.routes())
app.fire()

@yusukebe
Copy link
Member Author

yusukebe commented May 7, 2022

Hi @metrue

Thank you for telling your thinking. Something is the same as my thought, something is not the same.

This lines:

hono is like a connector
router is just another middleware

I think half right but half wrong. In the first place, I've made Hono as a router. The points:

  1. Hono is just a router actually.
  2. But, Hono is thin web framework that have Context to access Request/Response easily.
  3. Router what you call such like TrieRouter and RegExpRouter is just like a engine using inside of Hono
  4. To register the handler/middleware, use app.HTTP_METHOD like app.get app.post app.delete...

So, I can say that "Router" may not be a middleware. For another way, I made Route and app.route() in this PR to have a bunch of APIs.

const route = new Route()
route.get('/', (c) => c.text('foo')) // <--- stack the handler

const app = new Hono()
app.route('/users', route) // <--- add stacked handlers to app

Isn't this good?

@metrue
Copy link
Contributor

metrue commented May 7, 2022

Hey @yusukebe

Thank you so much for sharing, it may take me some time to fully understand, but that's fine, let's keep delivering and I think everything will be clear.

@yusukebe yusukebe merged commit fd4b4e1 into develop May 7, 2022
@yusukebe yusukebe deleted the feat/route branch May 7, 2022 13:22
@yusukebe yusukebe mentioned this pull request May 11, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants