The Command Line Interface (CLI) functionality includes automatically matching modules with controllers based on their names. For example, when you create modules and controllers in a different order using the CLI commands like:
nest g mo accounts
nest g mo contacts
nest g mo leads
nest g co leads
nest g co accounts
nest g co contacts
You will still see that the AccountsController
is placed in the AccountsModule
, the LeadsController
in the LeadsModule
, and the ContactsController
in the ContactsModule
.
If you want to add a controller to a module with a name different from the controller itself (e.g., adding the LeadsController
to the AccountsModule
), you can specify a custom path option at the end of the CLI command, like this:
nest g co leads accounts
The provided path should be relative to the directory specified as the root in your nest-cli.json
. This will result in creating a directory named accounts/leads
where the leads.controller.ts
file will reside, and the controller will be added to the AccountsModule
.
Nest framework defaults to adding the new controller to a module with the same name to facilitate feature module development. This approach simplifies CLI usage for developers, and if a specific module cannot be found, it will automatically add the controller to the AppModule
or the designated root module.