-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get the cats of a ninja #43
Comments
That's a good question, I'm looking for exactly the same. |
I am looking for the same. |
Please friend @shekohex , you are the nesting expert for routes and modules. |
Hello All. First, to achieve that task one must understand how express router works (since nest uses express-like adapters under the hood).
so in our ninja and cats example we have:
and what we need something like this:
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 // 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,
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 ! |
Brother @shekohex , thank you very much for your explanation. |
Let's say you add a route to get a ninja by Id. I assume this route does not need to be defined inside the
The endpoint for this route would be;
In this case, the The I am facing this error. How should I go about fixing this? |
Hi @hzburki, good question. Sorry I can't test this way at this moment, I'm using my mobile phone 📱 |
I'm submitting a...
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
Congratulations and thanks for your excellent work!
The text was updated successfully, but these errors were encountered: