nest g module [name]: generates new modulenest g controller [name]: generates new controllernest g service [name]: generates new serviceThe following sections will assume this directory structure:
app
|-- src/
|---- app.controller.ts
|---- app.module.ts
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>Running this command from the root app directory
nest g module bookswill result in the following:
app
|-- src/
+ |---- books/
+ |------ books.module.ts
|---- app.controller.ts
+ |---- app.module.ts <-- will add BooksModule to AppModule as import
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>Running this command from the root app directory
nest g controller bookswill result in the following:
app
|-- src/
|---- books/
+ |------ books.controller.ts
+ |------ books.module.ts <-- will add BooksController to BooksModule configuration
|---- app.controller.ts
|---- app.module.ts
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>Running this command from the root app directory
nest g service bookswill result in the following:
app
|-- src/
|---- books/
|------ books.controller.ts
+ |------ books.module.ts <-- will add BooksService to BooksModule configuration
+ |------ books.service.ts
|---- app.controller.ts
|---- app.module.ts
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>books.module.tsimport { Module } from '@nestjs/common';
import { BooksController } from './books.controller';
import { BooksService } from './books.service';
@Module({
controllers: [BooksController],
providers: [BooksService],
})
export class BooksModule {}books.controller.tsimport { Controller, Get } from '@nestjs/common';
import { BooksService } from './books.service';
@Controller('books')
export class BooksController {}books.service.tsimport { Injectable } from '@nestjs/common';
@Injectable()
export class BooksService {
findAll(): string {}
}app.module.tsimport { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { BooksModule } from './books/books.module';
@Module({
imports: [BooksModule], // this was added `nest g module books` command
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}