Koa: Can we mount express app into koa?

Created on 16 Dec 2015  路  7Comments  路  Source: koajs/koa

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

question

Most helpful comment

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

All 7 comments

Typically you can with the following sugar (Koa 1.x):

koaApp.use(function* (next) {
  yield expressApp.bind(null, this.req, this.res)
  yield next
})

Thanks for your answer, But I got something like

  TypeError: Cannot read property 'length' of null
      at pathtoRegexp (/.../node_modules/path-to-regexp/index.js:63:48)
      at new Layer (/.../node_modules/express/lib/router/layer.js:45:17)
      at Function.route (/.../node_modules/express/lib/router/index.js:494:15)
      at EventEmitter.app.(anonymous function) [as bind] (/.../node_modules/express/lib/application.js:480:30)

seems express think '.bind' is register a middleware that listen to 'BIND' http method.

I use node 5.2.0 by the way.

Hi @ericfong , interesting. Express 4.x doesn't even support Node.js 5.x officially yet, and it sounds like there is some kind of conflict with a new feature added in Node.js 5.1.0. You may need to downgrade to pre-5.1.0 to use Express, wait for a new version of Express that works with 5.1.0, or implement some kind of work-around in that code example, like the following:

koaApp.use(function* (next) {
  yield Function.prototype.bind.call(expressApp, null, this.req, this.res)
  yield next
})

I wouldn't advise it personally, Koa and Express have very different semantics, so you might find some strange bugs. If possible I'd just recommend using nginx or similar in front.

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

btw @ericfong this helped me a ton. thanks! got it working with koa.js app and agendash

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);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rainesinternationaldev picture rainesinternationaldev  路  5Comments

ke1Del picture ke1Del  路  3Comments

felixfbecker picture felixfbecker  路  5Comments

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments

sibelius picture sibelius  路  3Comments