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

Why does routing fail when asynchronous middleware is used with routing #1745

Closed
dong-lufei opened this issue Jan 31, 2023 · 2 comments
Closed
Labels

Comments

@dong-lufei
Copy link

import Koa from "npm:koa@2.14.1"
import Router from "npm:koa-router@12.0.0"

const app = new Koa()
const router = new Router()

app.use((ctx, next) => {
  const start = Date.now()
  return next().then(() => {
    const ms = Date.now() - start
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
  })
})
// koa 的中间件示例
function delay(duration) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve()
    }, duration)
  })
}
// 中间件1
app.use(async function (_ctx, next) {
  console.log(1)
  await next()
  console.log(4)
})

// 中间件2
app.use(async function (_req, _res, _next) {
  console.log(2)
  await delay(1000)
  console.log(3)
})

router.get("/", (ctx, _next) => {
  console.log("home")
  ctx.body = "home"
})

app.use(router.routes()).use(router.allowedMethods())

app.listen(1993, () => {
  console.log("http://localhost:1993")
})

deno run -A mod.js
http://localhost:1993
1
2
3
4
GET / - 1053ms

The page responds with 404 Not Found

@iwanofski
Copy link

iwanofski commented Sep 14, 2023

First of all, I just want to mention that Koa isn't tested against deno by the maintainers as far as I know so bugs might occur even though technically it "should just work" :)

But the reason this has unexpected response is because of this middleware:

// 中间件2
app.use(async function (_req, _res, _next) {
  console.log(2)
  await delay(1000)
  console.log(3)
})

That's not a function signature supported by Koa. Also there's nothing in that middleware calling next so logically the request isn't expected to reach your router (which is added to the middleware chain after said middleware).

@iwanofski
Copy link

I am going to assume this is either solved or stale, and so I will close this ticket. If you still have questions feel free to re-open this ticket!

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

No branches or pull requests

2 participants