API with NestJS #1. Controllers, routing and the module structure

177 阅读1分钟

Installation nestjs doc

npm i -g @nestjs/cli
nest new project-name
nest n nestjs-ts-wanago-tutorial 
use pnpm

cd nestjs-ts-wanago-tutorial 
mkdir app.module
mv app.* app.module

edit main.ts,

import { AppModule } from './app.module/app.module';
nest g res moudules/posts

# prevent to generating test files

nest g res moudules/posts --no-spec

edit src/app.module/app.module.ts, add posts module to AppModule's imports, every time you generate a new module, you should do it again.

//...
import { PostsModule } from '../modules/posts/posts.module';
@Module({
  imports: [PostsModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
pnpm start:dev

touch src/modules/posts/interface/post.interface.ts
// src/modules/posts/interface/post.interface.ts

export interface IPost {
  id: number;
  content: string;
  title: string;
}
The inferred type of 'UpdatePostDto' cannot be named without a reference to '.pnpm/@nestjs+common@10.0.0_reflect-metadata@0.1.13_rxjs@7.8.1/node_modules/@nestjs/common'. This is likely not portable. A type annotation is necessary.ts(2742)

if you encounter the error add

`preserveSymlinks: true` to `tsconfig.json`'s `compilerOptions` and press `command + shift + p` tppe and select `Developer: Reload Window` can fix it;
// src/modules/posts/dto/create-post.dto.ts
export class CreatePostDto {
  content: string;
  title: string;
}
// src/modules/posts/dto/update-post.dto.ts

import { PartialType } from '@nestjs/mapped-types';
import { CreatePostDto } from './create-post.dto';

export class UpdatePostDto extends PartialType(CreatePostDto) {
  id: number;
}

our src directory ends up like that

tree -I "node_modules|test"

.
├── README.md
├── dist
│   ├── app.module
│   │   ├── app.controller.d.ts
│   │   ├── app.controller.js
│   │   ├── app.controller.js.map
│   │   ├── app.module.d.ts
│   │   ├── app.module.js
│   │   ├── app.module.js.map
│   │   ├── app.service.d.ts
│   │   ├── app.service.js
│   │   └── app.service.js.map
│   ├── main.d.ts
│   ├── main.js
│   ├── main.js.map
│   ├── modules
│   │   └── posts
│   │       ├── dto
│   │       │   ├── create-post.dto.d.ts
│   │       │   ├── create-post.dto.js
│   │       │   ├── create-post.dto.js.map
│   │       │   ├── update-post.dto.d.ts
│   │       │   ├── update-post.dto.js
│   │       │   └── update-post.dto.js.map
│   │       ├── entities
│   │       │   ├── post.entity.d.ts
│   │       │   ├── post.entity.js
│   │       │   └── post.entity.js.map
│   │       ├── interface
│   │       │   ├── post.interface.d.ts
│   │       │   ├── post.interface.js
│   │       │   └── post.interface.js.map
│   │       ├── posts.controller.d.ts
│   │       ├── posts.controller.js
│   │       ├── posts.controller.js.map
│   │       ├── posts.module.d.ts
│   │       ├── posts.module.js
│   │       ├── posts.module.js.map
│   │       ├── posts.service.d.ts
│   │       ├── posts.service.js
│   │       └── posts.service.js.map
│   ├── moudules
│   └── tsconfig.build.tsbuildinfo
├── nest-cli.json
├── package.json
├── pnpm-lock.yaml
├── src
│   ├── app.module
│   │   ├── app.controller.spec.ts
│   │   ├── app.controller.ts
│   │   ├── app.module.ts
│   │   └── app.service.ts
│   ├── main.ts
│   └── modules
│       └── posts
│           ├── dto
│           │   ├── create-post.dto.ts
│           │   └── update-post.dto.ts
│           ├── entities
│           │   └── post.entity.ts
│           ├── interface
│           │   └── post.interface.ts
│           ├── posts.controller.spec.ts
│           ├── posts.controller.ts
│           ├── posts.module.ts
│           ├── posts.service.spec.ts
│           └── posts.service.ts
├── tsconfig.build.json
└── tsconfig.json

ref: source