Nest.js 入門の基本

インストール

# 如果是 node.js 10 之后的版本,需要加 --ignore-engines 来忽略一些不兼容库的警告
npm i -g @nestjs/cli --ignore-engines
# 或
yarn global add @nestjs/cli --ignore-engines

入門ドキュメント

チップ

非同期メソッドの最適化

不要な async/await ラッパーを減らします。サンプルコードは次のとおりです。

function test() {
    
    
  const deferred = {
    
    
    promise: undefined,
    resolve: undefined
  };
  deferred.promise = new Promise((resolve) => {
    
    
    deferred.resolve = resolve;
  });
  setTimeout(() => {
    
    
    deferred.resolve('hello world');
  }, 1000);
  return deferred.promise;
}

@Controller()
export class AppController {
    
    
  constructor(private readonly appService: AppService) {
    
    }

  // 以下两种写法都能运行,推荐使用第一种
  @Get('/test')
  getTest(): Promise<string> {
    
    
    return test();
  }

  @Get('/test2')
  async getTest2(): Promise<string> {
    
    
    return await test();
  }
}

同様に、コントローラーを除いて、モデル、サービス、その他の場所の内部メソッドも最適化できます。これは、 を呼び出すときに外側の層がすでにそれを持っているためですawait同時に、各メソッドのリターンの種類にも注意を払い、良い習慣を身に付ける必要があります。

Fastify フレームワークの使用

その部分に関するドキュメントはなく、サンプル プロジェクトのみです: https://github.com/nestjs/nest/tree/master/sample/10-fastify

yarn remove @nestjs/platform-express
yarn remove @types/express
yarn add @nestjs/platform-fastify

main.tsファイルを変更します。

import {
    
     NestFactory } from '@nestjs/core';
// 新增引用
import {
    
    
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import {
    
     AppModule } from './app.module';

async function bootstrap() {
    
    
  // 原有代码:
  // const app = await NestFactory.create(AppModule);
  // 替换代码:
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );
  await app.listen(3000);
  console.log(`Application is running on: ${
      
      await app.getUrl()}`);
}
bootstrap();

オープンAPI(Swagger)

現在、英語のドキュメントのみが存在し、中国語のドキュメントはありません。アドレス: https://docs.nestjs.com/openapi/introduction

Fastify と併用:

yarn add @nestjs/swagger
yarn add fastify-swagger
# [Nest] 7253   - 2020/07/08 下午4:42:59   [PackageLoader] The "fastify-swagger" package is missing. Please, make sure to install this library ($ npm install fastify-swagger) to take advantage of SwaggerModule. +37ms
# 如果出现类似报错,使用2.x版本重试
yarn add fastify-swagger@^2.6.0

サンプルコード:

import {
    
     NestFactory } from '@nestjs/core';
import {
    
    
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import {
    
     DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import {
    
     AppModule } from './app.module';

async function bootstrap() {
    
    
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );
  const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .addBearerAuth()
    .build();
  // 生成的 JSON 格式文档,可以导出静态化
  const document = SwaggerModule.createDocument(app, options);
  // 注入, 访问 http://localhost:3000/api 可以访问
  SwaggerModule.setup('api', app, document);

  await app.listen(3000);
  console.log(`Application is running on: ${
      
      await app.getUrl()}`);
}
bootstrap();

モジュール コードは、Express Swagger のサンプル プロジェクトを参照できます: https://github.com/nestjs/nest/tree/master/sample/11-swagger

Open-API.json ファイルのサンプル コードを生成します。

import {
    
     resolve } from 'path';
import {
    
     writeFileSync } from 'fs';

import {
    
     NestFactory } from '@nestjs/core';
import {
    
     FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import {
    
     DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import {
    
     AppModule } from './app.module';

async function bootstrap() {
    
    
  const app = (await NestFactory.create) < NestFastifyApplication > (AppModule, new FastifyAdapter());
  const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .addBearerAuth()
    .build();
  // 生成的 JSON 格式文档,可以导出静态化
  const document = SwaggerModule.createDocument(app, options);
  writeFileSync(resolve(__dirname, '../api.json'), JSON.stringify(document, null, 2), {
    
     encoding: 'utf8' });
}
bootstrap();

E2E テスト

まだドキュメントが見つかりません。テスト ソース コードの例を参照してください: https://github.com/nestjs/nest/blob/master/integration/hello-world/e2e/fastify-adapter.spec.ts

import {
    
     Test, TestingModule } from '@nestjs/testing';
import {
    
     AppModule } from './../src/app.module';
// 新增引用
import {
    
     FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import {
    
     expect } from 'chai';

describe('AppController (e2e)', () => {
    
    
  let app: NestFastifyApplication;

  beforeEach(async () => {
    
    
    const moduleFixture: TestingModule = await Test.createTestingModule({
    
    
      imports: [AppModule]
    }).compile();

    // 修改 app 创建
    app = moduleFixture.createNestApplication < NestFastifyApplication > new FastifyAdapter();

    await app.init();
  });

  it('/ (GET)', () => {
    
    
    // return request(app.getHttpServer())
    //   .get('/')
    //   .expect(200)
    //   .expect('Hello World!');

    // 改用 inject 方式,不用 supertest
    return app
      .inject({
    
    
        method: 'GET',
        url: '/'
      })
      .then(({
     
      payload }) => expect(payload).to.be.eql('Hello World!'));
  });
});

ロガー

npm i --save nestjs-pino
npm i --save-dev pino-pretty

main.ts エントリ ファイルは以下をインポートします。

import {
    
     Logger } from 'nestjs-pino';

const app = await NestFactory.create(MyModule, {
    
     logger: false });
app.useLogger(app.get(Logger));

app.module.ts ファイルには以下が導入されています。

import {
    
     LoggerModule } from 'nestjs-pino';

@Module({
    
    
  imports: [LoggerModule.forRoot()],
  controllers: [AppController],
  providers: [MyService]
})
class MyModule {
    
    }

コントローラーでの使用例:

import {
    
     Logger } from 'nestjs-pino';

@Controller()
export class AppController {
    
    
  constructor(private readonly myService: MyService, private readonly logger: Logger) {
    
    }

  @Get()
  getHello(): string {
    
    
    // pass message
    this.logger.log('getHello()');

    // also we can pass context
    this.logger.log('getHello()', AppController.name);

    return `Hello ${
      
      this.myService.getWorld()}`;
  }
}

または、以下を使用しますPinoLogger(推奨):

// my.service.ts
import {
    
     PinoLogger, InjectPinoLogger } from 'nestjs-pino';

@Injectable()
export class MyService {
    
    
  // regular injecting
  constructor(private readonly logger: PinoLogger) {
    
    }

  // regular injecting and set context
  constructor(private readonly logger: PinoLogger) {
    
    
    logger.setContext(MyService.name);
  }

  // inject and set context via `InjectPinoLogger`
  constructor(@InjectPinoLogger(MyService.name) private readonly logger: PinoLogger) {
    
    }

  getWorld(...params: any[]) {
    
    
    this.logger.info('getWorld(%o)', params);
    return 'World!';
  }
}

起動スクリプトの変更:

nest start --watch | pino-pretty

グラフキュール

に依存しますapollo-server

おすすめ

転載: blog.csdn.net/jslygwx/article/details/131871857