Welcome to this lesson on generating components using the NestJS CLI. In past lessons, we've dug deep into creating and managing various parts of a NestJS
application manually. We explored setting up a basic NestJS
application and delved into controllers, providers, and modules. By now, you should have a solid understanding of these core components and how they fit together.
In this lesson, we'll learn how to leverage the NestJS CLI
to speed up our development process by generating code boilerplate automatically. Our goal by the end of this lesson is to generate a new module, controller, and service for a "Books" feature using the CLI.
Let's start by introducing some specific CLI commands we'll be using: nest g module
, nest g controller
, and nest g service
.
nest g module [name]
: This command generates a new module. A module organizes your application into cohesive blocks of functionality.nest g controller [name]
: This command generates a controller, which handles incoming requests and returns responses to the client.nest g service [name]
: This command generates a service, which contains the business logic and can be injected into controllers or other services.
These commands help in scaffolding the code quickly and maintaining a clean structure in our NestJS
application.
Let's walk through the steps to generate a new module, controller, and service for a "Books" feature using the NestJS CLI
.
- Open a new terinal and change the directory
Bash
1cd app
-
Generate the Books Module:
Bash1nest g module books
This command will create a new directory called
books
with a filebooks.module.ts
inside it.Notice: This new
BooksModule
is automatically added to the rootAppModule
as an import!
-
Generate the Books Controller:
Bash1nest g controller books
This generates a file named
books.controller.ts
inside thebooks
directory.Notice: This new
BooksController
is automatically added to the newBooksModule
configuration.
-
Generate the Books Service:
Bash1nest g service books
This generates a file named
books.service.ts
inside thebooks
directory.Notice: This new
BooksService
is automatically added to the newBooksModule
configuration.
Now, let's review the files generated by the above commands and understand their contents.
TypeScript1import { Module } from '@nestjs/common'; 2import { BooksController } from './books.controller'; 3import { BooksService } from './books.service'; 4 5@Module({ 6 controllers: [BooksController], 7 providers: [BooksService], 8}) 9export class BooksModule {}
TypeScript1import { Controller, Get } from '@nestjs/common'; 2import { BooksService } from './books.service'; 3 4@Controller('books') 5export class BooksController {}
TypeScript1import { Injectable } from '@nestjs/common'; 2 3@Injectable() 4export class BooksService { 5 findAll(): string {} 6}
TypeScript1import { Module } from '@nestjs/common'; 2import { AppController } from './app.controller'; 3import { AppService } from './app.service'; 4import { BooksModule } from './books/books.module'; 5 6@Module({ 7 imports: [BooksModule], // This was added automatically 8 controllers: [AppController], 9 providers: [AppService], 10}) 11export class AppModule {}
In this lesson, we learned how to use the NestJS CLI
to generate essential components: modules, controllers, and services. This can significantly speed up your development process by providing pre-configured boilerplate code and maintaining a clean project structure.
Key Points Covered:
- The purpose and usage of
nest g module
,nest g controller
, andnest g service
. - How the CLI helps streamline the creation of modules, controllers, and services.
- Review and explanation of the generated code files.
Next, you'll get hands-on practice to reinforce these concepts. Try generating your own modules, controllers, and services to become more comfortable with the CLI. Happy coding!