Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map of middleware doesn't work #511

Open
polRk opened this issue Jun 22, 2019 · 3 comments
Open

Map of middleware doesn't work #511

polRk opened this issue Jun 22, 2019 · 3 comments

Comments

@polRk
Copy link

polRk commented Jun 22, 2019

node.js version: v12.2.0

npm/yarn and version: v6.9.0

koa-router version: ^7.4.0

koa version: ^2.7.0

Code sample:

import * as compose from 'koa-compose'
import { Middleware } from 'koa'
import * as Router from 'koa-router'

const server = new Koa<AppState, AppContext>()
const router = new Router<AppState, AppContext>()

middleware: Map<string, Middleware<AppState, AppContext>> = new Map()
middleware.set('first', async (ctx, next) => {
  await 10s ...
  await next()
})
middleware.set('second', async (ctx, next) => {
  await 20s ...
  await next()
})

router.use('/path', compose([...middleware.values()]))
server.use(router.routes())
server.use(async context => {
  context.body = 'OK'
})

Expected Behavior:

All handlers(middleware) should work

Actual Behavior:

No handler works

Log:

  <-- POST /path
  --> POST /path 200 6ms 2b
@polRk
Copy link
Author

polRk commented Jun 22, 2019

console.log([...middleware.values()]) // [ [AsyncFunction], [AsyncFunction] ]

@polRk
Copy link
Author

polRk commented Jun 22, 2019

But

server.middleware.push(...middleware.values()) // It's working

@polRk
Copy link
Author

polRk commented Jun 22, 2019

And this works

server.use(compose([...middleware.values()]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant