Lesson 5
Generating Components Using the CLI
Introduction to NestJS CLI

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.

Overview of CLI Commands

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.

Generating the Books Module, Controller, and Service

Let's walk through the steps to generate a new module, controller, and service for a "Books" feature using the NestJS CLI.

Start a new terminal to work
  1. Open a new terinal and change the directory
    Bash
    1cd app
Generate your Module
  1. Generate the Books Module:

    Bash
    1nest g module books

    This command will create a new directory called books with a file books.module.ts inside it.

    Notice: This new BooksModule is automatically added to the root AppModule as an import!

Generate the Controller
  1. Generate the Books Controller:

    Bash
    1nest g controller books

    This generates a file named books.controller.ts inside the books directory.

    Notice: This new BooksController is automatically added to the new BooksModule configuration.

Generate the Service
  1. Generate the Books Service:

    Bash
    1nest g service books

    This generates a file named books.service.ts inside the books directory.

    Notice: This new BooksService is automatically added to the new BooksModule configuration.

Code Review and Explanation

Now, let's review the files generated by the above commands and understand their contents.

books.module.ts (generated)
TypeScript
1import { 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 {}
books.controller.ts (generated)
TypeScript
1import { Controller, Get } from '@nestjs/common'; 2import { BooksService } from './books.service'; 3 4@Controller('books') 5export class BooksController {}
books.service.ts (generated)
TypeScript
1import { Injectable } from '@nestjs/common'; 2 3@Injectable() 4export class BooksService { 5 findAll(): string {} 6}
app.module.ts (generated)
TypeScript
1import { 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 {}
Summary and Next Steps

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, and nest 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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.