GithubHelp home page GithubHelp logo

Comments (6)

matthieusieben avatar matthieusieben commented on May 8, 2024 4

Thanks for your answer.

I have several use-cases for this:

  1. At my company we currently use Express. I want to push Koa because I think it is a more modern and overall better way to implement the middleware pattern. But my company cannot afford a full re-write. So the only solution is to push Koa as the replacement, and slowly migrate endpoints 1 by 1 to Koa. Doing this requires to use Koa as an Express middleware.

  2. Some libraries use Express under the hood. For example, Nuxt.js uses Express for its serverMiddlewares. As I said, I prefer using Koa to implement my middlewares.

  3. This is not coming from me, really. Here is an extract of Koa doc on koajs.com:

app.callback()
Return a callback function suitable for the http.createServer() method to handle a request. You may also use this callback function to mount your Koa app in a Connect/Express app.

The project's own doc recommend using callback() to mount a koa app into an express app. But this integration is currently broken due to this originalUrl problem.

Now I understand that you don't want to make changes to the Application's createContext() methods. And, to be honest, I don't think it is the right way either.

If there is one place where Koa's code should care about integration with another lib, it should be in the one place build for that purpose: the callback() method. Of course you can argue that this can be done in user-land, or even in yet another (one liner) NPM module. But then, you should remove any mention to Express from the doc, or at least mention that the integration with Express is broken in some cases. And I don't think that that is going to help Koa become a replacement for Express.

from discussions.

fl0w avatar fl0w commented on May 8, 2024 1

I don't see the point, why would you run Koa as a middleware for express? In addition, modify Koa source for an unsupported/unintended use-case?

from discussions.

matthieusieben avatar matthieusieben commented on May 8, 2024

A less breaking solution would be to add an option to restore the originalUrl when generating a callback:

  callback(options = {}) {
    const fn = compose(this.middleware)

    if (!this.listenerCount('error')) this.on('error', this.onerror)

    const handleRequest = (req, res) => {
      if (options.restoreUrl) req.url = req.originalUrl || req.url
      const ctx = this.createContext(req, res)
      return this.handleRequest(ctx, fn)
    }

    return handleRequest
  }

from discussions.

matthieusieben avatar matthieusieben commented on May 8, 2024

My current "hack" is to monkey patch the callback():

  const koaMiddleware = koaApp.callback()

  expressApp.use('/api', (req, res) => {
    req.url = req.originalUrl || req.url
    handleRequest(req, res)
  })

from discussions.

matthieusieben avatar matthieusieben commented on May 8, 2024

A more complex (?) alternative would be to make use of the req.baseUrl that the express router adds, when computing the originalUrl.

from discussions.

Frondor avatar Frondor commented on May 8, 2024

Hello! Is this still an issue? I see the source code looks like the changes suggested are already implemented.

I'm actually experiencing the same limitation while doing a forward rewrite.

from discussions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.