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
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
38
Go言語CLIツールで生産効率UPした話
ak2ie
0
110
Goではじめるバックエンド開発
ak2ie
0
64
Notion APIと学ぶNext.js
ak2ie
0
550
フロントエンドでDDDやってみた
ak2ie
0
76
初心者がシビックテックに参加してみた
ak2ie
0
110
Firebase についてとことん語りたい
ak2ie
0
110
D3.jsでグラフを描いてみた
ak2ie
0
110
Flutterはじめます
ak2ie
0
150
Other Decks in Programming
See All in Programming
Namespace and Its Future
tagomoris
6
710
Android 16 × Jetpack Composeで縦書きテキストエディタを作ろう / Vertical Text Editor with Compose on Android 16
cc4966
2
260
個人軟體時代
ethanhuang13
0
330
デザイナーが Androidエンジニアに 挑戦してみた
874wokiite
0
550
Laravel Boost 超入門
fire_arlo
3
220
複雑なフォームに立ち向かう Next.js の技術選定
macchiitaka
2
200
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
1
620
スケールする組織の実現に向けた インナーソース育成術 - ISGT2025
teamlab
PRO
1
150
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
2
770
速いWebフレームワークを作る
yusukebe
5
1.7k
実用的なGOCACHEPROG実装をするために / golang.tokyo #40
mazrean
1
290
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
The World Runs on Bad Software
bkeepers
PRO
70
11k
A designer walks into a library…
pauljervisheath
207
24k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
23
1.4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.7k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
Embracing the Ebb and Flow
colly
87
4.8k
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:エラーレスポンス変換