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.

get the cats of a ninja

See original GitHub issue

I’m submitting a…

[ ] Regression 
[ ] Bug report
[x] Feature request
[x] Documentation issue or request

Hello, I return my collection of ninjas with a get in /ninjas. I add a new ninja with a post in /ninjas. I consult and modify the data of a ninja in /ninjas/id.

The ninjas can have cats. I wish I could consult the cats of a ninja with a get in: /ninjas/id/cats add a new cat to a ninja with a post in /ninjas/id/cats I would also like to be able to consult and modify a ninja’s cat in /ninjas/id/cats/id

It’s possible?

I found this link that has something similar: #17

but if I do what is exposed there, the post and get methods stops working in /ninjas

How can I make everything work?

And for example in the URL /ninjas/12/cats/10

Get the data of the cat 10 and its owner (the ninja 12)

Congratulations and thanks for your excellent work!

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
shekohexcommented, Dec 29, 2018

Hello All. SPOILER ALERT ⚠️ : a long answer with a lot of explanations on the way 😂

First, to achieve that task one must understand how express router works (since nest uses express-like adapters under the hood). the way it works is so simple, so i will try to introduce it in a simple examples and in our context only, since we are discussing how to nest routes, i will talk about it a little bit:

the notion of examples will be HTTP_METHOD /routeControllerClass@routerHandler

so in our ninja and cats example we have:

  • GET /ninjaNinjaController@sayHello
  • GET /ninja/catsCatsController@sayHello

and what we need something like this:

  • GET /ninjaNinjaController@sayHello
  • GET /ninja/allNinjaController@getAllNinja
  • GET /ninja/:ninjaIdNinjaController@getNinjaById
  • GET /ninja/nested/catsCatsController@sayHello
  • GET /ninja/:ninjaId/catsCatsController@sayHello
  • GET /ninja/:ninjaId/cats/allCatsController@getAllCats
  • GET /ninja/:ninjaId/cats/:catIdCatsController@getCatById
  • POST /ninja/createNinjaController@createNinja
  • POST /ninja/:ninjaId/cats/createCatsController@createCat

note the order of the above routes, you will know the reason next 😃

that’s an easy task with nest & nest router

first, take this as a role of thump, Always put your static routes before the dynamic one, by static and dynamic here i mean, the route is static if it dose not have any params in it, eg: /ninja/all and it is said to be dynamic if it would have dynamic params as in /ninja/:ninjaId. That’s how express works in first place, the route matching will first try to match the requested route by the order they appears in the code, let’s assume for a moment that express somehow uses a Map<string, (req, res, next) ⇨ T>, and it calls Map#set whenever it sees a new route, so for example:

// the map keys.
// []
router.get('/ninja', ...); // ['ninja']
router.get('/ninja/cats', ...); // ['ninja', 'ninja/cats']
... // and so on.

the reason i told that you need to put your static routes before the dynamic is how express do it, for example: the user requested /ninja/5 as defined as /ninja/:ninjaId then the ninjaId = 5, right ? what if we have also added a /ninja/cats after that route ? Errors starts to happens, express will try to match the /ninja/:ninjaId instead of /ninja/cats/ and bind the ninjaId to cats, boom !

try this here: https://runkit.com/shekohex/express-routes101

so back to our issue, it’s now easy how we could achieve this

here is a way to do it

// routes.ts
import { Routes } from 'nest-router';
import { CatsModule } from './cats/cats.module';
import { DogsModule } from './dogs/dogs.module';
import { NinjaModule } from './ninja/ninja.module';
export const routes: Routes = [
  {
    path: '/ninja',
    module: NinjaModule,
    children: [
      { path: 'nested/cats', module: CatsModule }, // note how i added that before the next route ?!
      { path: ':ninjaId/cats', module: CatsModule }, // wanna play ? try to reverse order this route with 1st one
      { path: '/dogs', module: DogsModule },
    ],
  },
];

now in your controllers, you can do what you want, but remember the role ! you can see/test/play with a full working example here: https://github.com/shekohex/nest-router/tree/master/examples/nest-v5x-m2m with all of the above routes implemented and tested.

5reactions
IngAjVillaloncommented, Dec 30, 2018

Brother @shekohex , thank you very much for your explanation. This has served me well enough to be able to continue on. You are undoubtedly the master of routes and module nesting. Congratulations bro!

Read more comments on GitHub >

github_iconTop Results From Across the Web

32 Ninja Cats That Have Mastered The Ancient Art Of Ninjutsu
The ancient Japanese martial art of ninjutsu may now lie forgotten by many, but modern cats still carry the torch of the ninja,...
Read more >
Ninja Cat (Special Cat) | Battle Cats Wiki - Fandom
Ninja Cat is a Special Cat that can be purchased for 50 Cat Food in the Upgrade Menu after completing Philippines/Oita in Empire...
Read more >
Cat Ninja (Volume 1) - Amazon.com
From Epic! Originals, Cat Ninja is a hilarious graphic novel series about a lovable cat with a heroic alter-ego. Raised ...
Read more >
Cat Ninja Children's Book Collection - Epic
Cat Ninja · The Leading Digital Reading Platform for Kids · The only kids' eBook subscription service that offers thousands of high-quality books...
Read more >
Cat Ninja - TV Tropes
These cats often have all-black fur due to ninjas being generally portrayed as wearing all-black outfits. This need not apply solely to household...
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