Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
NestJSのはじめ方
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
ak2ie
April 28, 2023
Programming
0
140
NestJSのはじめ方
ak2ie
April 28, 2023
Tweet
Share
More Decks by ak2ie
See All by ak2ie
SVG完全に理解してグラフ書いてみた
ak2ie
0
47
Go言語CLIツールで生産効率UPした話
ak2ie
0
110
Goではじめるバックエンド開発
ak2ie
0
70
Notion APIと学ぶNext.js
ak2ie
0
570
フロントエンドでDDDやってみた
ak2ie
0
78
初心者がシビックテックに参加してみた
ak2ie
0
110
Firebase についてとことん語りたい
ak2ie
0
110
D3.jsでグラフを描いてみた
ak2ie
0
110
Flutterはじめます
ak2ie
0
150
Other Decks in Programming
See All in Programming
ノイジーネイバー問題を解決する 公平なキューイング
occhi
0
100
AIによる開発の民主化を支える コンテキスト管理のこれまでとこれから
mulyu
3
300
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1.2k
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
CSC307 Lecture 08
javiergs
PRO
0
670
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
270
疑似コードによるプロンプト記述、どのくらい正確に実行される?
kokuyouwind
0
390
AIで開発はどれくらい加速したのか?AIエージェントによるコード生成を、現場の評価と研究開発の評価の両面からdeep diveしてみる
daisuketakeda
1
2.5k
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
200
Raku Raku Notion 20260128
hareyakayuruyaka
0
240
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
690
OSSとなったswift-buildで Xcodeのビルドを差し替えられるため 自分でXcodeを直せる時代になっている ダイアモンド問題編
yimajo
3
620
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
430
Music & Morning Musume
bryan
47
7.1k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
940
Imperfection Machines: The Place of Print at Facebook
scottboms
269
14k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
420
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
55
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.2k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
190
Embracing the Ebb and Flow
colly
88
5k
Transcript
NestJSのはじめ方 2023/4/28
自己紹介 • 名前:ak2ie • システムエンジニア • 好きなもの:コーヒー
NestJSとは • Node.js上で動くバックエンドフレームワーク • TypeScriptでかける • CLIでファイルを生成可能 • ある程度決められたフォーマットに従って書くので、複数人で開発しやすい NestJSを知らない方向けに、どんな機能があるのかご紹介します
Controller NestJSの処理の流れ Middleware ログ Guard 認証 Pipe データ変換 Filter エラー制御
リクエストを処理したい!
Controller NestJSの処理の流れ
コントローラー(Controller) 【用途】 • リクエストをメインで処理 【定義方法】 • classにデコレーターをつけてController を定義 • メソッドにデコレーターをつけて処理を定
義 /cats をGETメソッドで呼び出す @Controller('cats') export class CatsController { @Get() findAll(): string { return 'This action returns all cats'; } } Controllerを定義 処理を定義
Controller • WebhookなどPOSTで呼び出された場 合、デフォルトではHTTPステータスコー ド201を返す • 200を返したい場合 @Post(‘webhook’) @HttpCode(200) hoge()
{ ……. } 例:LINE Messaging API
ログをとるには?
Controller NestJSの処理の流れ Middleware ログ
ミドルウェア(Middleware) 【用途(例)】 • ログ 【定義方法】 • @Injectableをつける • configureメソッドでセットアップ •
Expressのミドルウェアも使えます // 定義 @Injectable() export class LoggerMiddleware ... { } // 使い方 export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer .apply(LoggerMiddleware) .forRoutes('cats'); } }
認証するには?
Controller NestJSの処理の流れ Guard 認証
ガード(Guard) 【用途】 • 認証 【定義方法】 • @Injectableをつける • CanActivateメソッドで、true/falseを返 す
• @UseGuards()で使う @Injectable() export class AuthGuard implements CanActivate { canActivate( … ) { return true; } } @UseGuards(AuthGuard)
送られてきたデータを変換す るには?
Controller NestJSの処理の流れ Pipe データ変換
パイプ(Pipe) 【用途(例)】 • 送られてきたデータの変換 【定義方法】 • 標準のPipeもある • @Injectableをつけて、transformメソッド を実装する
• ParseIntPipe • ParseBoolPipe
エラー発生時のレスポンスを 変えたい!
Controller NestJSの処理の流れ Filter エラー制御
フィルタ(Filter) 【用途(例)】 • エラー発生時の返却値を制御 【実装方法】 • コントローラーでエラーをCatchして、 NestJSのHttpExceptionを投げる try {
await this.service.findAll() } catch (error) { throw new HttpException({ status: HttpStatus.FORBIDDEN, error: 'This is a custom message', }, HttpStatus.FORBIDDEN, { cause: error }); }
テストはかけるよね?
テスト • テストの雛形を作成可能 • Jestなど好きなツールを利用可能 • DI(依存性の注入)によりテスト実行が容 易 // 準備
const moduleRef = await Test.createTestingModule({ // テスト用モジュール }).compile(); catsController = moduleRef.get(CatsController); // テスト expect(await catsController.findAll()).toBe(result);
ところで、API定義書が ほしいんだけど?
OpenAPI(Swagger) • 実装に沿ったAPI定義書を表示できる • デコレータでサンプルデータを書くと、 モックサーバのレスポンスを定義できる http://localhost:3000/api
NestJSまとめ • Node.js上で動くバックエンドフレームワーク • TypeScriptでかける • テストのサポート、API定義書表示 • Controller:デコレーターで対応するHTTPメソッド等を定義 •
Middleware:ログ • Guard:認証 • Pipe:リクエストデータ変換 • Controller:処理 • Filter:エラーレスポンス変換