Skip to content
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

Child routes with parent modules #82

Open
btd1337 opened this issue Jun 21, 2020 · 2 comments
Open

Child routes with parent modules #82

btd1337 opened this issue Jun 21, 2020 · 2 comments

Comments

@btd1337
Copy link

btd1337 commented Jun 21, 2020

How to use child routes with parent modules?

The only way I managed to make the child route work was:

// app.routes.ts

export const routes: Routes = [{
        path: 'banks',
	module: BanksModule,
	children: [
	    {
	        path: 'account',
		module: BankAccountsModule,
	    },
	],
}];
@Module({
    imports: [
        RouterModule.forRoutes(routes),
        BanksModule
        BankAccountsModule      // Child module is imported directly in AppModule
     ]
})
export class AppModule {}
app.module
        | banks.module
        | bankaccounts.module
```ts
@Controller('/banks')
export class BanksController implements CrudController<Bank> {
@Controller()
export class BankAccountsController implements CrudController<BankAccount> {

This http request below return all bank accounts

GET /banks/account

but...

GET /banks/

return:

{
    "statusCode": 404,
    "message": "Cannot GET /banks",
    "error": "Not Found"
}

All other ways return `/banks` but `banks/account` returns:

```json
{
    "statusCode": 400,
    "message": "Invalid param id. UUID string expected",
    "error": "Bad Request"
}




Using nested module

but if you try to use nested module, it will to fail.

@Module({
    imports: [
        RouterModule.forRoutes(routes),
        BanksModule
     ]
})
export class AppModule {}
@Module({
	imports: [
             BankAccountsModule      // Child module is imported in your parent module
       ]
})
export class BanksModule {}
app.module
        | banks.module
                | bankaccounts.module

I already tried to remove and add tags to BankAccount Controller but nothing makes it work the second way.

I think it is the right way to implement nested routes with parent modules.

But how to make this work with nest-router?


Update: I think that is any error with Nestjs Crud library.

@gvonhagel
Copy link

Any update on this? Did you find a solution cause I'm having the same problem and I don't wanna redefine all my nested modules in AppModule.

@j1i-ian
Copy link

j1i-ian commented Mar 26, 2022

when you assign value to @Controller, nest router will add as suffix to path.

try request to /banks/account/banks.

@Controller('/banks') should be @Controller().

or you can specify app.routes.ts like this



export const routes: Routes = [{
        path: '/',
	module: BanksModule,
	children: [
	    {
	        path: 'account',
		module: BankAccountsModule,
	    },
	],
}];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants