NestJS with NomadCoder
27351 ワード
1. Basic
1.プロジェクトのインストールと作成
詳細については、
$ npm i -g @nestjs/cli
nest new project-name
git remote add origin https://github.com/damin0320/hi-nest
2.構造
1.たぶん?
mainに行くかAPPmodule->moduleかcontroller->controllerか?
あ、たぶんこういう仕組みでしょうか?
2.構造とアーキテクチャ
2.REST API(映画APIの作成)
1.コントロールの作成:url
nest g co
이후 이름짓기
import { Controller, Get } from '@nestjs/common';
@Controller('movies')
export class MoviesController {
@Get()
getAll(){
return 'This will return all movies';
}
}
import { Module } from '@nestjs/common';
import {MoviesController} from './movies/movies.controller'
@Module({
imports: [],
controllers: [MoviesController],
providers: [],
})
export class AppModule {}
結果
映画部分がルーターになります.
idでurlを制御したら?
@Get("/:id")
getOne(){
return "This will return one movie"
}
}
「何か必要なものがあれば、お願いしなければなりません」
@Get("/:id")
getOne(@Param('id') id: string){
return `This will return one movie with the id : ${id}`;
}
@Post()
create(){
return "This will create a movie"
}
@Delete("/:id")
remove(@Param('id') movieId:string){
return `This will delete a movie with the id : ${movieId}`};
2.より多くのルート(装飾器)
忘れてはいけない
「何か必要なものがあれば、お願いしなければなりません」
直接調べると?
@Get("search")
search(@Query("year") searchingYear: string){
return `We are searching for a movie made after: ${searchingYear}`;
}
3.サービス:論理管理
作成
export class Movie{
id : number;
title:string;
year: number;
genres:string[];
}
後続コントローラ、実装サービス(get,post,delete;後続コードはハブ、ストリームのみ)
id値をpostでインクリメントし、入力をGET(Bypostman)と確認する
アドレスに異常が発生した場合の処理異常
getOne(id:string):Movie{
const movie = this.movies.find(movie=>movie.id=== +id);
if(!movie){
throw new NotFoundException(`Moive wiht ID : ${id} NOT FOUND`);
}
return movie;
}
.....
deleteOne(id:string) {
this.getOne(id)
this.movies.filter(movie=>movie.id !== +id);
}
(controller)
import { Controller, Get, Param, Post, Delete, Patch, Body, Query } from '@nestjs/common';
import {MoviesService} from './movies.service'
import {Movie} from './entities/movie.entity'
@Controller('movies')
export class MoviesController {
constructor(private readonly moviesService: MoviesService){}
@Get()
getAll(): Movie[]{
return this.moviesService.getAll();
}
@Get(":id")
getOne(@Param('id') movieId: string) : Movie{
return this.moviesService.getOne(movieId);
}
@Post()
create(@Body() movieData){
return this.moviesService.create(movieData)
}
@Delete(":id")
remove(@Param('id') movieId:string){
return this.moviesService.deleteOne(movieId);}
@Patch(':id')
patch(@Param('id') movieId: string, @Body() updateData) {
return this.moviesService.update(movieId, updateData);
}
}
(service)import { Injectable, NotFoundException } from '@nestjs/common';
import {Movie} from './entities/movie.entity';
@Injectable()
export class MoviesService {
private movies : Movie[] = [];
getAll() : Movie[]{
return this.movies;
}
getOne(id:string):Movie{
const movie = this.movies.find(movie=>movie.id=== +id);
if(!movie){
throw new NotFoundException(`Moive wiht ID : ${id} NOT FOUND`);
}
return movie;
}
deleteOne(id:string) {
this.getOne(id)
this.movies.filter(movie=>movie.id !== +id);
}
create(movieData){
this.movies.push({
id:this.movies.length + 1,
...movieData
});
}
update(id:string, updateData){
const movie = this.getOne(id);
this.deleteOne(id);
this.movies.push({...movie,...updateData});
}
}
盲点:ハッカーなどの検証検査で発見される可能性があります.
しかし、Nest JSで解決できる方法があります.
Reference
この問題について(NestJS with NomadCoder), 我々は、より多くの情報をここで見つけました https://velog.io/@sinichy7/NestJS-with-NomadCoderテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol