koajs/mount

koa-mount + koa2 + koa-convert + koa-router

amitport opened this issue · 17 comments

I know you got a lot of issue on koa2. I went over them. There are 2 concrete suggestions:

1 - use koa-convert - does not work when the mounted app use koa-router@next.
2 - use https://github.com/coderhaoxin/mount/tree/next-by-promise - works but not published to npm which is bad for forward compatibility.

would you consider reviewing the workflow with koa-convert or re-introducing the 'next-by-promise' branch as a published npm lib/tag?

provide a runnable example for #1

does not work when the mounted app use koa-router@next

this:

const mountedApp = new Koa();

const router = Router();
router.get('/', (ctx) => {
        ctx.body = 'throws type error'
});
mountedApp.use(router.routes());

...

app.use(convert(mount(mountedApp)));

will throw when accessing '/':

TypeError: next is not a function
      at Object.dispatch ([...]\node_modules\koa-router\lib\router.js:315:32)
      at Object.<anonymous> ([...]\node_modules\koa-mount\node_modules\koa-compose\index.js:26:19)
      at next (native)
      at onFulfilled ([...]\node_modules\co\index.js:65:19)
      at [...]\node_modules\co\index.js:54:5
      at new Promise ([...]\users\node_modules\core-js\modules\es6.promise.js:197:7)
      at Object.co ([...]\node_modules\co\index.js:50:10)
      at converted ([...]\node_modules\koa-convert\index.js:17:15)
      at dispatch ([...]\node_modules\koa-compose\index.js:41:32)
      at [...]\node_modules\koa-compose\index.js:34:12
      at Server.<anonymous> ([...]\node_modules\koa\lib\application.js:126:7)
      at emitTwo (events.js:100:13)
      at Server.emit (events.js:185:7)
      at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:537:12)
      at HTTPParser.parserOnHeadersComplete (_http_common.js:89:23)

where's koa-convert?

import convert from 'koa-convert';
import Router from 'koa-router';
import mount from 'koa-mount';

ah, my bad. missed the fn call on the bottom- sorry, skimmed it haha.

gyson commented

Current version of koa-mount does not support Koa 2 but above code makes it to accept Koa 2 app instance at app.use(convert(mount(mountedApp))), which results that mount() returns a broken middleware (producing TypeError).

Current koa-mount only accepts Koa 1 app and returns Koa 1 middleware.

Ideally, we should have koa-mount@next which will accept Koa 2 app and return Koa 2 middleware and you can mount without koa-convert like this app.use(mount(mountedApp)). (https://github.com/coderhaoxin/mount/tree/next-by-promise did this)

@coderhaoxin In this case, since koa-convert doesn't 100% do the trick, I'd suggest that we publish your promisified version of koa-mount w/ the next tag on npm for koa@2 users.

@amitport In the meantime, you can use a Git URI in NPM. I'd fork @coderhaoxin's changes into your own fork / repo so that if he deletes his branch, your builds don't fail :P

@tejasmanohar I wil change the examples and test to async&await in my own next-by-promise branch

  1. examples: async&await is the end-users use cases normally IMO.
  2. tests: not for end-users, so compiled is fine?

Any thought? And then, it's suitable to be published IMO (need npm right 😄)

👍 100% agree @coderhaoxin

@tejasmanohar or anyone can review this? coderhaoxin/koa-mount#next :)
If OK, I'll push to koajs/mount#next

is this moving forward to support Koa 2?

@jonathanong I need the right on npm :)

added

+1, thanks @coderhaoxin!

npm dist-tag ls koa-mount
latest: 1.3.0
next: 2.0.0

Beautiful, thanks again!