I am trying to get my routing to work in nestjs for multiple routes, however I can't seem to get the routes to map correctly, this is what I want to achieve:
/user/:id/purchase-transaction/:id
I have generated a user module and a user/purchase-transaction module
But both got registered as:
/user/:id routes and
/purchase-transaction/:id routes
So the question is how to connect the two?
What I have tried to do is, create a Routes array:
const routes: Routes = [
{
path: '/purchase-transaction/',
module: PurchaseTransactionModule,
},
];
@Module({
imports: [PurchaseTransactionModule, RouterModule.register(routes)],
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
Inside purchase-transaction.controller I modified the @Controller() to be /
And now this maps to:
UserController {/user}
{/user, POST}
{/user, GET}
{/user/:id, GET}
{/user/:id, PATCH}
{/user/:id, DELETE}
PurchaseTransactionController {/purchase-transaction}
{/purchase-transaction, POST}
{/purchase-transaction, GET}
{/purchase-transaction/:id, GET}
{/purchase-transaction/:id, PATCH}
{/purchase-transaction/:id, DELETE}
Instead I want the PurchaseTransactionController to map to:
PurchaseTransactionController {/user/:id/purchase-transaction}
{/user/:id/purchase-transaction, POST}
{/user/:id/purchase-transaction, GET}
{/user/:id/purchase-transaction/:id, GET}
{/user/:id/purchase-transaction/:id, PATCH}
{/user/:id/purchase-transaction/:id, DELETE}
Which I achieved by modifying my routes:
const routes: Routes = [
{
path: '/user/:id/purchase-transaction/',
module: PurchaseTransactionModule,
},
];
However, I see two problems with this, first is that now I have two params that are :id and second is that, well I already have the UserController is it not possible to extend on top of it?
What would be the correct way of doing this, and should I not use the default :id identifier in this case and rename them to :userId and :transactionId?