How to create services and controllers in NestJS modules with command line?

William D'Andrea
2 min readApr 25, 2023

NestJS is a backend framework that aims to create a clean and maintainable service architecture over time.

However, some practices can quickly lead to very hard to understand and unmaintainable code.
In this story, we will look at how to create a clean NestJS architecture.

Creating the module

A module will be an ‘entity’ of our NestJS project. We put a business domain in it.

For example, in a complex application, we might well have an authentication module, an account manager module, and a quiz module.
Each module has a different business domain, and it is important to separate our application in this way.

To create a module using NestJS and the NestJS CLI (command line), we just enter this command:

nest g mo <name-of-the-module>

For example, if we want to create the authentication module, we use this command

nest g mo authentication

Creating a controller in our module

As a reminder, generally speaking, a controller is where we put the different routes of our API.

We could have a POST /login route or a POST /logout route. To create this controller, here is the general command:

nest g co controllers/<controller-name>…

--

--

William D'Andrea

SaaS builder - Following me is like entering my brain, because here I share everything I know - Software Architect Engineer & Business Student