honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
16.83k stars 464 forks source link

[Discussion] A 'listen' function for Hono seems a good pattern? #2358

Open metrue opened 3 months ago

metrue commented 3 months ago

What is the feature you are proposing?

@Hono/node-server is really great lib for the Web Standard web framework to handle IncomingMessage, and I'm using it in EdgeQL, and implementation like this,

import { serve } from '@hono/node-server'
import { EdgeQL } from '../'

export class NodeEdgeQL extends EdgeQL {
  listen(
    { port, hostname }: { port?: number; hostname?: string },
    listener?: (info: { address: string; family: string; port: number }) => void
  ): void {
    serve(
      {
        fetch: this.fetch,
        port: port ?? 3000,
        hostname: hostname ?? '0.0.0.0',
      },
      listener
    )
  }
}

Then we start a server just just call listen by invoking instance function.

const app = new Hono()
app.listen(3000, (info) => { })
yusukebe commented 3 months ago

Hi @metrue !

Sorry if my understanding is wrong.

I think adding a listen method to Hono is not good. If listen is a method for "server", then Hono should not have it. This is because Hono makes the "server" a black box. I think the server role is done by Cloudflare workerd, Deno or Bun runtime, or Node Adapter.