Comments (6)
Thanks for your answer.
I have several use-cases for this:
-
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.
-
Some libraries use Express under the hood. For example,
Nuxt.js
uses Express for itsserverMiddlewares
. As I said, I prefer using Koa to implement my middlewares. -
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.
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.
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.
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.
A more complex (?) alternative would be to make use of the req.baseUrl
that the express router adds, when computing the originalUrl
.
from discussions.
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)
- Improving the Koa ecosystem by moving all Koa modules to the koajs organization! HOT 62
- Immutable empty query obj HOT 15
- How to exclude api router? HOT 1
- Access to XMLHttpRequest at 'https://localhost:3443/images' from origin 'http://localhost:4200' has been blocked by CORS policy
- Destroy Mounts
- Conditional middleware
- Support for spa server?
- how to reg two part to the same file
- learning koa 2 and updating examples:https://github.com/aline2013/koa2-examples
- how to set interval within stream
- Hosting mp3 file
- Either 404 or wrong content HOT 1
- Error jump URL when ctx.path is directory
- koa-static: heavy caching issue HOT 2
- Why do i need koa-static HOT 1
- 404 not found for everything HOT 2
- koajs/conditional-get: uws.http failure
- Using both koa-compress and koa-static , extremely slow when visit static files from 127.0.0.1 or localhost (from public address is fast), why? HOT 2
- Brotli compress is slow HOT 2
- @fl0w becomes @miwnwski HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from discussions.