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

Support for Express.js Compatible Middleware #3293

Open
dipakparmar opened this issue Aug 18, 2024 · 9 comments · May be fixed by honojs/middleware#928
Open

Support for Express.js Compatible Middleware #3293

dipakparmar opened this issue Aug 18, 2024 · 9 comments · May be fixed by honojs/middleware#928
Labels
enhancement New feature or request.

Comments

@dipakparmar
Copy link

I'm exploring the possibility of supporting Express.js compatible middleware in Hono. This would allow for easier migration from Express to Hono and enable the use of the vast ecosystem of Express middleware within Hono applications.

Proposed Implementation

I'm looking for something like this for backward compatibility:

type ExpressMiddleware = (
  req: IncomingMessage,
  res: ServerResponse,
  next: (err?: any) => void
) => void

export function adaptExpressMiddleware(middleware: ExpressMiddleware): MiddlewareHandler {
  return async (c: Context, next: Next) => {
    const req = c.req.raw as unknown as IncomingMessage
    const res = c.res as unknown as ServerResponse

    await new Promise<void>((resolve, reject) => {
      middleware(req, res, (err) => {
        if (err) reject(err)
        else resolve()
      })
    })

    await next()
  }
}

const app = new Hono()
app.use(adaptExpressMiddleware(existingexpressmiddleware()));

Current Issues

So far, I haven't been able to make this work due to type incompatibility issues. The main challenges are:

  1. Hono's Context object doesn't directly map to Express's req and res objects.
  2. The IncomingMessage and ServerResponse types from Node.js http module don't align perfectly with Hono's request and response handling.

Questions

  1. Is it feasible to implement such an adapter in Hono?
  2. What would be the best approach to handle the type mismatches between Express and Hono?
  3. Are there any performance implications we should consider when adapting Express middleware?

Goals

  • Enable the use of popular Express middleware in Hono applications.
  • Provide a smooth migration path for developers moving from Express to Hono.
  • Maintain Hono's performance advantages while offering this compatibility.

Any insights, suggestions, or assistance on how to achieve this would be greatly appreciated. Thank you!

@yusukebe
Copy link
Member

Hi @dipakparmar

This is a super exciting proposal, though I don't know if we should do it in our official repo like honojs/middleware or hono/node-server. I think it is more prefer discussing it in the honojs/hono repo. So I'll transfer it.

@yusukebe yusukebe transferred this issue from honojs/middleware Aug 18, 2024
@yusukebe yusukebe added the enhancement New feature or request. label Aug 18, 2024
@yusukebe
Copy link
Member

A quick response.

I think we can also follow the Connect specs: https://github.com/senchalabs/connect

@dipakparmar
Copy link
Author

A quick response.

I think we can also follow the Connect specs: senchalabs/connect

Thanks @yusukebe, https://github.com/senchalabs/connect looks interesting, I'll check it out.

@Nicolab
Copy link

Nicolab commented Nov 26, 2024

Maybe with connect and node-mocks-http like this: https://github.com/kravetsone/elysia-connect-middleware/blob/main/src/index.ts

@wangyingang
Copy link

This is very valuable, I will keep following it.

@harrytran998
Copy link

Yeah, I think the gap for people who can choose a modern server-side framework now is the compatibility issue with the current middleware's expressjs. Especially with serious business, we always prefer feature-rich sets and battle-tested middleware. Express was good at this with the framework's age + large community.

I think the Honojs team can choose some of the most popular middleware of express, like cookie-parser, morgan, helmet
for examples first, then we can improve incrementally it.

@QizhengMo
Copy link

Hi team, any progress on this?
Even a limited support will be a big help, and I believe the community will help improving it.

@EdamAme-x
Copy link
Contributor

I will challenge this

@EdamAme-x
Copy link
Contributor

Most of the work has been completed.
I will try to see if I can fix the bugs during the winter break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants