Open11

NestJSに入門する

yuiyui
Which package manager would you ❤️  to use? >> yarnを選択
yuiyui
$ git clone https://github.com/nestjs/typescript-starter.git project

でもできるらしい(結果は同じぽい)
yarn startできたので次〜

yuiyui

ファイルを確認する

src
 app.controller.spec.ts // 単一ルート用の基本となるコントローラー
 app.controller.ts // コントローラーのユニットテスト
 app.module.ts // ルートモジュール、ここで依存関係を解決するぽい
 app.service.ts // シングルメソッドを書く
 main.ts // NestFactory?というのを使ってインスタンスを作るらしい
yuiyui

main.tsが何をしてるのかイマイチなので作られたファイルを読む

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

NestFactoryの型ファイルは以下

export declare class NestFactoryStatic {
    private readonly logger;
    private abortOnError;
    private autoFlushLogs;
    /**
     * Creates an instance of NestApplication.
     *
     * @param module Entry (root) application module class
     * @param options List of options to initialize NestApplication
     *
     * @returns A promise that, when resolved,
     * contains a reference to the NestApplication instance.
     */
    create<T extends INestApplication = INestApplication>(module: any, options?: NestApplicationOptions): Promise<T>;
    /**
     * Creates an instance of NestApplication with the specified `httpAdapter`.
     *
     * @param module Entry (root) application module class
     * @param httpAdapter Adapter to proxy the request/response cycle to
     *    the underlying HTTP server
     * @param options List of options to initialize NestApplication
     *
     * @returns A promise that, when resolved,
     * contains a reference to the NestApplication instance.
     */
    create<T extends INestApplication = INestApplication>(module: any, httpAdapter: AbstractHttpAdapter, options?: NestApplicationOptions): Promise<T>;
    /**
     * Creates an instance of NestMicroservice.
     *
     * @param module Entry (root) application module class
     * @param options Optional microservice configuration
     *
     * @returns A promise that, when resolved,
     * contains a reference to the NestMicroservice instance.
     */
    createMicroservice<T extends object>(module: any, options?: NestMicroserviceOptions & T): Promise<INestMicroservice>;
    /**
     * Creates an instance of NestApplicationContext.
     *
     * @param module Entry (root) application module class
     * @param options Optional Nest application configuration
     *
     * @returns A promise that, when resolved,
     * contains a reference to the NestApplicationContext instance.
     */
    createApplicationContext(module: any, options?: NestApplicationContextOptions): Promise<INestApplicationContext>;
    private createNestInstance;
    private initialize;
    private handleInitializationError;
    private createProxy;
    private createExceptionProxy;
    private createExceptionZone;
    private registerLoggerConfiguration;
    private createHttpAdapter;
    private isHttpServer;
    private setAbortOnError;
    private createAdapterProxy;
}
/**
 * Use NestFactory to create an application instance.
 *
 * ### Specifying an entry module
 *
 * Pass the required *root module* for the application via the module parameter.
 * By convention, it is usually called `ApplicationModule`.  Starting with this
 * module, Nest assembles the dependency graph and begins the process of
 * Dependency Injection and instantiates the classes needed to launch your
 * application.
 *
 * @publicApi
 */
export declare const NestFactory: NestFactoryStatic;

要はNestFactoryなるものの中にcreate関数があってINestApllicationを返すらしい。
この辺はあんまりもうよくわかってないけど、要はHTTPリスナーを作ってるだけぽい。

yuiyui

expressとfastifyは最初に入ってるのね。
fastifyがわかんないけどexpressと似たようなもんでしょ

yuiyui

main.tsでポート3000を指定してるので、サーバーを起動するとポート3000で開く

yuiyui

Controllerではルーティングを定義することができる

catsのコントローラーを作ってみる

$ nest g controller cats

src配下にcatsフォルダができてその中にコントローラーとスペックファイルができた

yuiyui

specファイルも一緒にできるのは高ポイント

yuiyui

空のコントローラーが出来上がるのですべてのネッコをreturnする関数を追加する

export class CatsController {
  @Get()
  findAll(): string {
    return 'This action returns all cats';
  }
}
yuiyui

これはこのエンドポイントにGETリクエストが行われると、Nestがユーザ定義のfindAll()メソッドにルーティングすることを指している。
findAllという名前は何でも良いらしい。見やすいようにというだけで命名してるだけで、実際は即時関数みたいな感じかな?