Complete sample
Use several available decorators to create a basic controller that exposes several methods of accessing and manipulating internal data.
/ * * *@description: Basic controller example with regular CRUD *@update: the 2021-09-10 13:59:16 *@author: Ada.H
*/
// cats.controller.ts
import { Controller, Get, Query, Post, Body, Put, Param, Delete } from '@nestjs/common';
import { CreateCatDto, UpdateCatDto, ListAllEntities } from './dto';
@Controller('cats')
export class CatsController {
@Post(a)create(@Body() createCatDto: CreateCatDto) {
return 'Add a cat';
}
@Get(a)findAll(@Query() query: ListAllEntities) {
return ` query${query.limit}Cats `;
}
@Get(':id')
findOne(@Param('id') id: string) {
return 'The query ID is${id}Cat information ';
}
@Put(':id')
update(@Param('id') id: string.@Body() updateCatDto: UpdateCatDto) {
return 'update id is${id}Cat information ';
}
@Delete(':id')
remove(@Param('id') id: string) {
return 'kill with id as${id}The cat `; }}Copy the code
The last step
Controllers are always modules. With your controllers in place, you need to include the array of controllers in the @Module() decorator.
Follow the official advice: keep each module in its own dedicated directory. The code is as follows:
// cats.module.ts
import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';
@Module({
controllers: [CatsController],
providers: [CatsService]
})
export class CatsModule {}
Copy the code
// app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CatsModule } from './cats/cats.module';
@Module({
imports: [CatsModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Copy the code
CRUD generator
Nest CLI provides a generator that automatically generates all of this template code, avoiding manual setup and making the development experience much simpler.
nest g resource
# add parameter --no-spec does not generate test files
nest g resource cats --no-spec
Copy the code
Figure 1: Screenshot of CMD running command effect
Note: Nest G Resource must be executed in the Commond panel of the system.
Git bash failed to run nest G resource. It is also impossible to select package installer (NPM or YARN), controller type (HTTP controller, microservice controller, GraphQL processor (code first or principle first), and WebSocket gateway via arrow keys.
Figure 2: Nest G Resource run generate template file screenshot
Execute this command in the project root directory to generate resources, not only for all Nest build files (module, service, controller classes), but also for entity classes, DTO classes, and test (.spec) files.
Nest CLI supports the generation of all CRUD terminal placeholders (REST API paths, GraphQL queries and compilation, message subscribers for microservices and WebSocket gateways).
The generated resource classes are not bound to any particular ORM (or data source) to be generic under any project.