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.

Prefix not work when pass router to another router using r.route

See original GitHub issue

I pass router to router using r.use and prefix work fine. But when I try to pass using r.route prefix not work, I think it is because router delegate work to @koa/router.

Can you handler this problem that I can you r.route with prefix and add documentation: “Passing through routers and be able use prefix”

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
3imed-jabericommented, May 2, 2021

@Roman-Kirnos, you can create a util or helper file then add this fucntion to it.

// helper/util file
function routesWrap (router, PREFIX) {
  return router.routes.map(route => ({ 
    ...route,
    path: (PREFIX + route.path).slice(0, -1) 
  }))
}

Now, it’s shorter.

const Router = require('koa-joi-router');

const events = Router();
events.prefix(EVENTS_PREFIX_ROUTE);
events.get('/', { validate: validator.getAll }, events.getAll);

const apiV1 = Router();
apiV1.prefix('/api/v1');
apiV1.route(routesWrap(events, EVENTS_PREFIX_ROUTE)); // WORK

module.exports = apiV1;
1reaction
3imed-jabericommented, Apr 28, 2021

You can use the Prefix path as constant like this;

const EVENTS_PREFIX_ROUTE = '/events'

Then use it like that and every thing work fine.

const Router = require('koa-joi-router');

const events = Router();
events.prefix(EVENTS_PREFIX_ROUTE);
events.get('/', { validate: validator.getAll }, events.getAll);

const apiV1 = Router();
apiV1.prefix('/api/v1');
apiV1.route(events.routes.map(route => ({ ...route, path: (EVENTS_PREFIX_ROUTE + route.path).slice(0, -1) }))); // WORK

module.exports = apiV1;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Including router which has a Mount on "/" does not work #4435
The mount with an empty path is in violation of 2. Passing a prefix would solve this issue in most cases however when...
Read more >
Nested Gorilla Mux router does not work - Stack Overflow
I know this question is somewhat old, but I have spent some time figuring out how handlers and matching work in go.
Read more >
Troubleshoot Border Gateway Protocol Routes that Do Not ...
This document describes how to troubleshoot when a Border Gateway Protocol (BGP) router does not announce BGP routes to peers.
Read more >
Routers - Django REST framework
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for...
Read more >
Path Prefix Middleware in Go - Calhoun.io
ServeMux] to wrap specific path prefixes in middleware ... func main() { r := mux. ... This works because the sirMuxalot router won't...
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