ZijianHe / koa-router

Router middleware for koa.
MIT License
4.85k stars 408 forks source link

Map of middleware doesn't work #511

Open polRk opened 5 years ago

polRk commented 5 years ago

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 commented 5 years ago
console.log([...middleware.values()]) // [ [AsyncFunction], [AsyncFunction] ]
polRk commented 5 years ago

But

server.middleware.push(...middleware.values()) // It's working
polRk commented 5 years ago

And this works

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