I have multiple modules and want to club them under one route path based on module
example.com/api/moduleA/controller1
example.com/api/module1/controller2
example.com/api/module1/controller3
example.com/api/module2/controller1
example.com/api/module2/controller2
example.com/api/module2/controller3
In index we can setup the root path
const containerConfig = await InversifyConfigContainer();
let routerConfig = express.Router({caseSensitive: false});
const app = express();
const server = new InversifyExpressServer(
containerConfig,
routerConfig,
{
rootPath: '/api'
},
app);
We have userRouter
import { NextFunction, Request, Response } from 'express';
import { inject } from 'inversify';
import { controller, httpPost, httpPut } from 'inversify-express-utils';
import { ApiOperationPost, ApiPath } from 'swagger-express-ts';
import TYPES from '@ioc/constant/Types';
import DtoRouteValidationMiddleware from '@shared-infra/http/middleware/DtoRouteValidationMiddleware';
import { UserDTO } from '@user-module/application/dtos/UserDto';
import { CreateUserController } from '../controller/CreateUserController';
import { UpdateUserController } from '../controller/UpdateUserController';
@ApiPath({
name: 'Users',
path: '/user'
})
@controller('/user')
export abstract class UserRouters {
constructor(
@inject(TYPES.CreateUserController)
private readonly _createUserController: CreateUserController,
@inject(TYPES.UpdateUserController)
private readonly _updateUserController: UpdateUserController
) {}
@ApiOperationPost({
description: 'Create New User',
summary: 'Creating the new user for todo application ',
parameters: {
body: { description: 'Create New User', required: true, model: 'User' }
},
responses: {
200: { description: 'Success' },
400: { description: 'Something fails' }
}
})
@httpPost('/', DtoRouteValidationMiddleware(UserDTO))
public async createUser(
request: Request,
response: Response,
next: NextFunction
) {
return this._createUserController.execute(request, response, next);
}
@httpPut('/:id')
public async updateUser(
request: Request,
response: Response,
next: NextFunction
) {
return this._updateUserController.execute(request, response, next);
}
}
I want configure that path at top instead of writing again in all the child route as
moduleAcontroller1.ts
@controller('/moduleA/controller1')
moduleAcontroller2.ts
@controller('/moduleA/controller2')
Instead I am figuring how to do
module1Parent.ts // this should hold based path for moduleA
moduleAcontroller1.ts
@controller('/controller1')
moduleAcontroller2.ts
@controller('/controller2')
Lets say user, todo are related to moduleA and other features xyz, pqr related to moduleB
How can we make a parent route for moduleA which takes the path of example.com/api/moduleA
and then pass all the child routes like user and todo under it, similarly for moduleB which should further handle route example.com/api/moduleB/xyz
example.com/api/moduleB/pqr