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
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
LambdaなのにVM?Lambda MicroVMsを調べてみた / About Lambda Micro VMs
ak2ie
2
73
DDDのエッセンスを取り入れたAIでの開発
ak2ie
1
320
Claude Codeはレガシー移行でどこまで使えるのか?
ak2ie
1
1.7k
SVG完全に理解してグラフ書いてみた
ak2ie
0
76
Go言語CLIツールで生産効率UPした話
ak2ie
0
140
Goではじめるバックエンド開発
ak2ie
0
96
Notion APIと学ぶNext.js
ak2ie
0
610
フロントエンドでDDDやってみた
ak2ie
0
95
初心者がシビックテックに参加してみた
ak2ie
0
140
Other Decks in Programming
See All in Programming
あなたはMCPの機能を、まだ知らない。
4geru
1
700
Enforcing Your Architecture with Unit Tests in the AI Era (Swift Connection X)
steliosf
PRO
1
190
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
200
Beyond Chatbots: Agentic UI with Open Standards @W-JAX 2026, München
manfredsteyer
PRO
0
120
AIがコードを書いてくれる時代、我々は何を確かめるのか
yosuke_furukawa
PRO
13
6.1k
自宅サーバはどこまで再現できるのか - Proxmox VE上のVM環境をIaCとバックアップで戻してみた
youto
0
430
AI-Powered Domain-Driven Design
ziobrando
0
470
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
340
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
360
Goコンパイラを作ってGoをもっと理解する / Build a Go compiler to better understand Go
ikura_hamu
0
130
SODA - FACT BOOK(JP)
sodainc
1
40k
仕様駆動開発入門 (何を作るかを先に決めて渡す) | 目指せ Kiro マスター 05
inariku
PRO
2
210
Featured
See All Featured
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
530
Typedesign – Prime Four
hannesfritz
42
3.2k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
780
Chasing Engaging Ingredients in Design
codingconduct
0
400
The Cost Of JavaScript in 2023
addyosmani
56
10k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
GraphQLとの向き合い方2022年版
quramy
50
15k
The Invisible Side of Design
smashingmag
301
52k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
570
WCS-LA-2024
lcolladotor
0
1k
4 Signs Your Business is Dying
shpigford
187
23k