shfshanyue / Daily-Question

互联网大厂内推及大厂面经整理,并且每天一道面试题推送。每天五分钟,半年大厂中
https://q.shanyue.tech
4.94k stars 511 forks source link

【Q604】判断以下路由,将会响应哪一个路由 #621

Open shfshanyue opened 3 years ago

shfshanyue commented 3 years ago

代码见: 多匹配路由 - codesandbox

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

router.get('/', (ctx, next) => {
  ctx.body = 'hello, world'
})

router.get('/api/users/10086', (ctx, next) => {
  console.log(ctx.router)
  ctx.body = {
    userId: 10086,
    direct: true
  }
})

router.get('/api/users/:userId', (ctx, next) => {
  console.log(ctx.router)
  ctx.body = {
    userId: ctx.params.userId
  }
})

app.use(router.routes())
shfshanyue commented 3 years ago

TODO