question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Router.use() doesn't take in composed Router middleware as expected

See original GitHub issue

It’s just a quick mirror of https://github.com/ZijianHe/koa-router/issues/454, which is unresolved but closed.

node.js version: 10.16.0

npm/yarn and version: yarn 1.17.3

@koa/router version: 8.0.6

koa version: 2.11.0

koa-compose version: 4.1.0

Code sample:

const app = new Koa();
const router1 = new Router();
const router2 = new Router();

router2.get('/', ctx => {
  ctx.body = 'not work';
});

router1.use('/', compose([router2.routes(), router2.allowedMethods()]));

app.use(router1.routes()).use(router1.allowedMethods());

Expected Behavior:

Visit '/', get 'not work'

Actual Behavior:

Visit '/', get 'Not Found'

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lagdencommented, Mar 7, 2020

@meteorlxy

It’s not necessary use compose.
You can see the @dominicegginton example above.

I make this way to put my routes together (using compose):
https://github.com/lagden/api-boilerplate/blob/master/server/routes/index.js

1reaction
cmd-johnsoncommented, Jul 11, 2021

I got @koa/router to work with koa-compose like this:

const app = new Koa()
const router1 = new Router()
const router2 = new Router()

router2.get('/', ctx => {
  ctx.body = 'this works!'
})
const composed = Object.assign(
  compose([router2.routes(), router2.allowedMethods()]),
  { router: router2 }
)

router1.use('/', composed)
app.use(router1.routes()).use(router1.allowedMethods())

I’m pretty sure middleware.router belongs to the internal API of @koa/router and as such may be changed without notice, but for now this appears to be working perfectly fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

router.use middleware does not get called - Stack Overflow
The middleware that doesn't get called is jwtPermission. So I have a routes folder where I have a index, and the authentication file(that...
Read more >
Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >
Routing - 4.x - CakePHP Cookbook
Routing ¶. class Cake\Routing\RouterBuilder¶. Routing provides you tools that map URLs to controller actions. By defining routes, you can separate how your ...
Read more >
Directives • Akka HTTP - Documentation
Routes effectively are simply highly specialised functions that take a ... So, when using Akka HTTP's Routing DSL you should almost never have...
Read more >
typeerror: router.use() requires a middleware function but got ...
TypeError : Router.use() requires middleware function but got a Object. And tracing the error leads me to line 32 in app.js. TraceMSG. at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found