get the cats of a ninja
See original GitHub issueI’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:
- Created 5 years ago
- Reactions:3
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
so in our ninja and cats example we have:
GET
/ninja
⇨NinjaController@sayHello
GET
/ninja/cats
⇨CatsController@sayHello
and what we need something like this:
GET
/ninja
⇨NinjaController@sayHello
GET
/ninja/all
⇨NinjaController@getAllNinja
GET
/ninja/:ninjaId
⇨NinjaController@getNinjaById
GET
/ninja/nested/cats
⇨CatsController@sayHello
GET
/ninja/:ninjaId/cats
⇨CatsController@sayHello
GET
/ninja/:ninjaId/cats/all
⇨CatsController@getAllCats
GET
/ninja/:ninjaId/cats/:catId
⇨CatsController@getCatById
…POST
/ninja/create
⇨NinjaController@createNinja
POST
/ninja/:ninjaId/cats/create
⇨CatsController@createCat
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 dynamicparams
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 aMap<string, (req, res, next) ⇨ T>
, and it callsMap#set
whenever it sees a new route, so for example: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 theninjaId = 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 theninjaId
tocats
, boom !so back to our issue, it’s now easy how we could achieve this
here is a way to do it
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.
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!