question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can we mount express app into koa?

See original GitHub issue

Can we have something like

koaApp.use(expressToKoaMount('/prefix', expressAppOrHttpServer))

Tried implement that with on-finished(https://github.com/jshttp/on-finished) But got many header is set error

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
ericfongcommented, Dec 17, 2015

Finally, it works by: (mounting kue-ui into koa app)

// kue-ui
const expressApp = express()
const prefix = '/kue-ui/'
expressApp.use(prefix, kueUiExpressApp)
koaApp.use(function*(next) {
  // do routing by simple matching, koa-route may also work
  if (this.path.startsWith(prefix)) {
    // direct to express
    if (this.status === 404 || this.status === '404') {
      delete this.res.statusCode
    }
    // stop koa future processing (NOTE not sure it is un-doc feature or not?)
    this.respond = false
    // pass req and res to express
    expressApp(this.req, this.res)
  } else {
    // go to next middleware
    yield next
  }
})

A bit hacky, hope kue-ui can have a koa / promise version

3reactions
adipascucommented, Nov 18, 2018

I used this snippet to use express 4 inside koa 2

  const Koa = require('koa');
  const app = new Koa();
  app.use(async ctx => {
    ctx.status = 200;  // this is required to prevent incorrect codes returned after express runs
    ctx.respond = false;
    express_app(ctx.req, ctx.res);
  });

  app.listen(3000);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate Your Express App to Koa 2.0 - JScrambler Blog
Get to know more about migrating your Express App to Koa, a more evolved form of Express whose main feature is the elimination...
Read more >
Mount Koajs app on top of Express - node.js - Stack Overflow
Now I have an Express app that already starts its own http server. How can I mount a koa app on top of...
Read more >
koa-mount - npm
koa -mount ... Mount other Koa applications as middleware. The path passed to mount() is stripped from the URL temporarily until the stack...
Read more >
Koa - next generation web framework for node.js
Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust...
Read more >
Creating a Modular Koa Application | Codementor
An experiment on how you can separate the application routes to create a modular directory structure for your application.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found