ホームページ >ウェブフロントエンド >jsチュートリアル >NESTJSバックエンドの概要
NestJS: 堅牢なバックエンド アプリケーションを構築するための包括的なガイド
NestJS は、効率的でスケーラブルなサーバー側アプリケーションを構築するための進歩的な Node.js フレームワークです。 TypeScript を活用して、オブジェクト指向、関数型、リアクティブ プログラミング パラダイムをシームレスに統合します。このガイドでは、NestJS のコア機能と高度な機能の詳細な概要を説明します。
NestJS アプリケーションは、関連するサービス、コントローラー、プロバイダーをカプセル化する自己完結型ユニットであるモジュールを使用して構造化されています。 @Module()
デコレーターはこれらのモジュールを定義し、コードの編成と保守性を促進します。 すべての NestJS プロジェクトはルート モジュール (通常は AppModule
) から始まります。
例:
<code class="language-typescript">import { Module } from '@nestjs/common'; import { UsersService } from './users.service'; import { UsersController } from './users.controller'; @Module({ controllers: [UsersController], providers: [UsersService], }) export class UsersModule {}</code>
NestJS は依存関係の注入を広範囲に利用します。 モジュール内に登録されたプロバイダーはコントローラーや他のサービスに挿入され、疎結合とテスト容易性が保証されます。
例:
<code class="language-typescript">import { Injectable } from '@nestjs/common'; import { HttpService } from '@nestjs/axios'; @Injectable() export class UsersService { constructor(private readonly httpService: HttpService) {} }</code>
コントローラーは、受信リクエストとアプリケーション ロジックの間のインターフェイスとして機能します。 @Controller()
デコレーターはコントローラーを定義し、@Get()
、@Post()
などのデコレーターは HTTP メソッドを特定のハンドラー関数にマップします。
例:
<code class="language-typescript">import { Controller, Get } from '@nestjs/common'; @Controller('users') export class UsersController { @Get() findAll() { return 'All users'; } }</code>
サービスは、ビジネス ロジックとデータ アクセス操作をカプセル化します。 @Injectable()
デコレータは依存関係注入の対象としてマークを付けます。
例:
<code class="language-typescript">import { Injectable } from '@nestjs/common'; @Injectable() export class UsersService { private users = [{ id: 1, name: 'John Doe' }]; findAll() { return this.users; } }</code>
ミドルウェア機能はリクエストとレスポンスをインターセプトし、ロギングや認証などの横断的な問題を考慮します。 これらは @Injectable()
と app.use()
を使用して実装されます。
例: (例示 - 適切な設定が必要です)
<code class="language-typescript">import { Injectable, NestMiddleware } from '@nestjs/common'; @Injectable() export class LoggerMiddleware implements NestMiddleware { use(req, res, next) { console.log('Request logged:', req.method, req.url); next(); } }</code>
インターセプターはコントローラーの実行前または後にデータを変換します。 これらは NestInterceptor
を実装し、@UseInterceptors()
を使用して適用されます。
例: (例示)
<code class="language-typescript">import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; @Injectable() export class TransformInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe(map(data => ({ data, timestamp: new Date().toISOString() }))); } }</code>
プロバイダーは注入可能なコンポーネントです。 デフォルトのスコープはシングルトン (アプリケーションごとに 1 つのインスタンス) です。 リクエストまたは一時的なスコープは、カスタム動作用に定義できます。
例 (カスタムプロバイダー):
<code class="language-typescript">import { Module } from '@nestjs/common'; import { UsersService } from './users.service'; import { UsersController } from './users.controller'; @Module({ controllers: [UsersController], providers: [UsersService], }) export class UsersModule {}</code>
NESTJSは、コンポーネントの初期化とアプリケーションの起動を管理するためにOnModuleInit
やOnApplicationBootstrap
などのライフサイクルフックを提供します。
5。設計パターンとフォルダー構造@nestjs/confignestjsは、モジュール設計、固体原理の順守、およびデータ転送のためにDTOの使用を奨励しています。 推奨されるフォルダー構造は、元の入力で提供されています。
この拡張された応答は、NESTJのより詳細で整理された説明を提供し、読みやすさと明確さを改善しながら、コア情報を維持します。 特定のセクションの詳細は、リクエストに応じて提供できます。以上がNESTJSバックエンドの概要の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。