albatrosary's blog

UI/UXとエンタープライズシステム

NestJS のプロジェクト作成時にやること

  • NestJS CLI のインストール
% npm i -g @nestjs/cli
  • NestJS 関連のプロジェクト生成
% nest new project-name
% npm i nest-router -S
% npm i class-transformer class-validator -S
  • GCP 用の package.json
{
  "name": "",
  "version": "0.0.1",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "node main.js"
  },
  "dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/platform-express": "^7.0.0",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.12.2",
    "nest-router": "^1.0.9",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4"
  }
}
  • Github Actions の設定

省略

  • main.ts の変更
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  const port = process.env.PORT || 3000;
  await app.listen(3000);
}
bootstrap();
  • app.routes.ts の追加

nest g 無し

import { Routes } from 'nest-router';
import { AppModule } from './app.module';
import { ApiModule } from './api/api.module';

export const routes: Routes = [
  {
    path: '/',
    module: AppModule,
    children: [{ path: '/api/v1', module: ApiModule }],
  },
];
  • リソースの追加
% nest g module api
% nest g controller api/users
% nest g service api/users
% nest g interface api/users/user
% nest g class api/users/userDto

etc.