Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
NestJSのはじめ方
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
ak2ie
April 28, 2023
Programming
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
NestJSのはじめ方
ak2ie
April 28, 2023
Transcript
25 slides
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:エラーレスポンス変換
More Decks by ak2ie
See All by ak2ie
DDDのエッセンスを取り入れたAIでの開発
ak2ie
1
320
Claude Codeはレガシー移行でどこまで使えるのか?
ak2ie
1
1.7k
SVG完全に理解してグラフ書いてみた
ak2ie
0
75
Go言語CLIツールで生産効率UPした話
ak2ie
0
140
Goではじめるバックエンド開発
ak2ie
0
96
Notion APIと学ぶNext.js
ak2ie
0
610
フロントエンドでDDDやってみた
ak2ie
0
95
初心者がシビックテックに参加してみた
ak2ie
0
140
Firebase についてとことん語りたい
ak2ie
0
140
Other Decks in Programming
See All in Programming
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
300
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
240
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
130
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
16
9k
クラウドエージェントは、 何がそんなに便利なのか / what makes cloud agents so useful
rkaga
5
830
まずはコードより業務と向き合う:テーブル解体のためにPdMを呼んだ話
sakawe_ee
0
210
Seven New Principles of Software Engineering
yegor256
0
480
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
250
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
340
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
1.2k
小さなアプリを作って公開するまで
onozaty
0
170
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
170
Featured
See All Featured
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Paper Plane
katiecoart
PRO
6
55k
HDC tutorial
michielstock
2
1k
Six Lessons from altMBA
skipperchong
29
4.6k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
330
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
48k
Mind Mapping
helmedeiros
1
440
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Technical Leadership for Architectural Decision Making
baasie
4
640
Music & Morning Musume
bryan
48
7.5k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
330