ããã¯ããªã«ãããããŠæžãããã®ïŒ
Node.jsã®ãµãŒããŒãµã€ããã¬ãŒã ã¯ãŒã¯ãšããã°ãExpressãFastifyããããæåãªã®ããªãšæã£ãŠããã®ã§ããã
- Express - Node.js web application framework
- Fastify, Fast and low overhead web framework, for Node.js
ãThe State of JS 2021ãããJavaScript Rizing StarsããèŠãŠããŠãNest.jsãšãããã®ãç¥ã£ãã®ã§ã¡ãã£ãšè©ŠããŠãããããªããšã
The State of JS 2021: Back-end Frameworks
The State of JS 2021: バックエンドフレームワーク
「The State of JS 2021」公開。最も使われているフロントエンドのライブラリはReact、バックエンドはExpress、ビルドツールはwebpackなど - Publickey
NestJS
NestïŒNestJSïŒã¯ããµãŒããŒãµã€ãã¢ããªã±ãŒã·ã§ã³ã®ããã®ãã¬ãŒã ã¯ãŒã¯ã§ãã
Hello, nest!
A progressive Node.js framework for building efficient, reliable and scalable server-side applications.
NestJS - A progressive Node.js framework
ããã¥ã¡ã³ãã¯ãã¡ãã
Documentation | NestJS - A progressive Node.js framework
ãµã³ãã«ã¯ãã¡ãã
https://github.com/nestjs/nest/tree/master/sample
NestJSã«ã¯ã以äžã®ãããªç¹åŸŽããããŸãã
- TypeScriptã®ãµããŒã
- JavaScriptã§ãéçºå¯èœ
- OOPãFPãFRPã®çµã¿åããã䜿çš
- å éšã®å®è¡ãšã³ãžã³ãExpressïŒããã©ã«ãïŒãšFastifyããéžæå¯èœ
Controllerã®æžãæ¹ãèŠãŠãããšãïŒJavaããã䜿ã人ãšããŠã¯ïŒã©ããã§èŠãããšããããããªããšããããããªæ°ãããã®ã§ããâŠã
Controllers | NestJS - A progressive Node.js framework
ã¢ãŒããã¯ãã£ãŒã¯ãAngularã«å€§ãã圱é¿ãåããŠãããšãããŠããŸãã
The architecture is heavily inspired by Angular.
å®è¡ç°å¢ã¯ãNode.js 10.13.0以äžã§ããã°ããã¿ããã§ãã
Please make sure that Node.js (>= 10.13.0, except for v13) is installed on your operating system.
ãšãããããä»åã¯é°å²æ°ãç¥ãããã«ç°¡åã«åãããŠã¿ãããšæããŸãã
ãã¡ãã®First stepsã«æ²¿ã£ãŠé²ããŠã¿ãŸãããã
First steps | NestJS - A progressive Node.js framework
ç°å¢
ä»åã®ç°å¢ã¯ããã¡ãã
$ node --version v16.14.2 $ npm --version 8.5.0
NestJSãããžã§ã¯ããäœæãã
NestJSãããžã§ã¯ãã®äœææ¹æ³ã¯ãNest CLIãäœ¿ãæ¹æ³ãšstarterãããžã§ã¯ããã¯ããŒã³ãããã®ã©ã¡ããã«ãªãããã§ãã
â»çµæã¯åãã ããã§ã
Nest CLIã䜿ãå Žåã¯npm i -g @nestjs/cli
ã§ã€ã³ã¹ããŒã«ãããããªã®ã§ãããã¡ãã£ãšåŸ®åŠãªã®ã§npx
ã§å®è¡ããæ¹æ³ã§ããããšæããŸãã
远èšïŒ
ãã¡ãã®æ¹æ³ãåç
§ã
npxコマンドを使って、未インストールのコマンドを使った時の挙動を確認する - CLOVER🍀
ãšããããããããžã§ã¯ãçšã®ãã£ã¬ã¯ããªãäœæ
$ mkdir nestjs-getting-started $ cd nestjs-getting-started
次ã«ãNestJSã®CLIãã€ã³ã¹ããŒã«ã
$ npm i -D @nestjs/cli
ã€ã³ã¹ããŒã«ãããããŒãžã§ã³ã¯ããã¡ãã
$ npx nest --version 8.2.4
CLIã®ããã¥ã¡ã³ãã¯ããã¡ãã
Overview - CLI | NestJS - A progressive Node.js framework
nest new
ãŸãã¯nest generate
ã§ãããžã§ã¯ããäœæããããã§ãããã©ã¡ãã䜿çšãããã§ãããžã§ã¯ãã®æ§é ãå€ããããã§ãã
new
ã§äœæããå Žåã¯æšæºã¢ãŒããgenerate
ã§äœæããå Žåã¯ã¢ãã¬ãã¢ãŒãããšãªãããã§ãã
ä»åã¯æšæºã¢ãŒãã䜿ãããšã«ããŸãã
ãã«ãã¯ä»¥äžã®ããã«CLIå šäœãš
$ npx nest --help Usage: nest <command> [options] Options: -v, --version Output the current version. -h, --help Output usage information. Commands: new|n [options] [name] Generate Nest application. build [options] [app] Build Nest application. start [options] [app] Run Nest application. info|i Display Nest project details. update|u [options] Update Nest dependencies. add [options] <library> Adds support for an external library to your project. generate|g [options] <schematic> [name] [path] Generate a Nest element. Schematics available on @nestjs/schematics collection: âââââââââââââââââ¬ââââââââââââââ¬âââââââââââââââââââââââââââââââââââââââââââââââ â name â alias â description â â application â application â Generate a new application workspace â â class â cl â Generate a new class â â configuration â config â Generate a CLI configuration file â â controller â co â Generate a controller declaration â â decorator â d â Generate a custom decorator â â filter â f â Generate a filter declaration â â gateway â ga â Generate a gateway declaration â â guard â gu â Generate a guard declaration â â interceptor â in â Generate an interceptor declaration â â interface â interface â Generate an interface â â middleware â mi â Generate a middleware declaration â â module â mo â Generate a module declaration â â pipe â pi â Generate a pipe declaration â â provider â pr â Generate a provider declaration â â resolver â r â Generate a GraphQL resolver declaration â â service â s â Generate a service declaration â â library â lib â Generate a new library within a monorepo â â sub-app â app â Generate a new application within a monorepo â â resource â res â Generate a new CRUD resource â âââââââââââââââââŽââââââââââââââŽâââââââââââââââââââââââââââââââââââââââââââââââ
åãµãã³ãã³ãã«å¯ŸããŠèŠãããšãã§ããŸãã
$ npx nest new --help Usage: nest new|n [options] [name] Generate Nest application. Options: --directory [directory] Specify the destination directory -d, --dry-run Report actions that would be performed without writing out results. -g, --skip-git Skip git repository initialization. -s, --skip-install Skip package installation. -p, --package-manager [package-manager] Specify package manager. -l, --language [language] Programming language to be used (TypeScript or JavaScript). -c, --collection [collectionName] Schematics collection to use. --strict Enables strict mode in TypeScript. -h, --help Output usage information.
package.json
ãæ®ã£ããŸãŸã ãšãnew
ã§å€±æããã®ã§1床åé€ã
$ rm package*
æ®ã£ãŠãããšããããªããŸãâŠã
$ npx nest new . â¡ We will scaffold your app in a few seconds.. Error: A merge conflicted on path "/package.json". at /path/to/nestjs-getting-started/node_modules/@angular-devkit/schematics/src/tree/host-tree.js:141:35 at Array.forEach (<anonymous>) at HostTree.merge (/path/to/nestjs-getting-started/node_modules/@angular-devkit/schematics/src/tree/host-tree.js:130:23) at MapSubscriber.project (/path/to/nestjs-getting-started/node_modules/@nestjs/schematics/node_modules/@angular-devkit/schematics/src/rules/base.js:54:103) at MapSubscriber._next (/path/to/nestjs-getting-started/node_modules/rxjs/internal/operators/map.js:49:35) at MapSubscriber.Subscriber.next (/path/to/nestjs-getting-started/node_modules/rxjs/internal/Subscriber.js:66:18) at TapSubscriber._next (/path/to/nestjs-getting-started/node_modules/rxjs/internal/operators/tap.js:65:26) at TapSubscriber.Subscriber.next (/path/to/nestjs-getting-started/node_modules/rxjs/internal/Subscriber.js:66:18) at ThrowIfEmptySubscriber._next (/path/to/nestjs-getting-started/node_modules/rxjs/internal/operators/throwIfEmpty.js:44:26) at ThrowIfEmptySubscriber.Subscriber.next (/path/to/nestjs-getting-started/node_modules/rxjs/internal/Subscriber.js:66:18) Failed to execute command: node @nestjs/schematics:application --name=. --directory=undefined --no-dry-run --no-skip-git --no-strict --package-manager=undefined --language="ts" --collection="@nestjs/schematics"
éåžžã¯ãnest new [ã¢ããªã±ãŒã·ã§ã³å]
ã§æå®ããŸãããã¢ããªã±ãŒã·ã§ã³åã.
ã«ããããšã§ã«ã¬ã³ããã£ã¬ã¯ããªã察象ã«ã§ããŸãã
$ npx nest new .
äœæããããã¡ã€ã«ã衚瀺ãããããã±ãŒãžãããŒãžã£ãŒã®éžæãæ±ããããŸãã
â¡ We will scaffold your app in a few seconds.. CREATE .eslintrc.js (631 bytes) CREATE .prettierrc (51 bytes) CREATE README.md (3339 bytes) CREATE nest-cli.json (64 bytes) CREATE package.json (2011 bytes) CREATE tsconfig.build.json (97 bytes) CREATE tsconfig.json (546 bytes) CREATE src/app.controller.spec.ts (617 bytes) CREATE src/app.controller.ts (274 bytes) CREATE src/app.module.ts (249 bytes) CREATE src/app.service.ts (142 bytes) CREATE src/main.ts (208 bytes) CREATE test/app.e2e-spec.ts (630 bytes) CREATE test/jest-e2e.json (183 bytes) ? Which package manager would you â€ïž to use? (Use arrow keys) ⯠npm yarn pnpm
ä»åã¯npmãéžæã
ãã°ããåŸ ã£ãŠãããšãå®äºããŸãã
â Installation in progress... â ð Successfully created a new project ð Get started with the following commands: $ cd . $ npm run start Thanks for installing Nest ð Please consider donating to our open collective to help us maintain this package. ð· Donate: https://opencollective.com/nest
npm run start
ããã°ããã¿ããã§ããã
ã¡ãªã¿ã«ã察話圢åŒã§ã¯ãªãã³ãã³ãã©ã€ã³ãªãã·ã§ã³ã§æå®ããããã°ã以äžã®ãããªæãã«ãªããŸãã
$ npx nest new --language TypeScript --package-manager npm [ã¢ããªã±ãŒã·ã§ã³å]
--language
ã¯ä»äžããªããŠãããã©ã«ãã§TypeScriptãªã®ã§ããããªããšãªãâŠã
nest new
åŸã®ãã£ã¬ã¯ããªã¯ããããªæãã§ãã
$ ll åèš 648 drwxrwxr-x 6 xxxxx xxxxx 4096 3æ 22 23:59 ./ drwxrwxr-x 3 xxxxx xxxxx 4096 3æ 22 23:54 ../ -rw-rw-r-- 1 xxxxx xxxxx 631 3æ 22 23:55 .eslintrc.js drwxrwxr-x 7 xxxxx xxxxx 4096 3æ 22 23:56 .git/ -rw-rw-r-- 1 xxxxx xxxxx 391 3æ 22 23:56 .gitignore -rw-rw-r-- 1 xxxxx xxxxx 51 3æ 22 23:55 .prettierrc -rw-rw-r-- 1 xxxxx xxxxx 3339 3æ 22 23:55 README.md -rw-rw-r-- 1 xxxxx xxxxx 64 3æ 22 23:55 nest-cli.json drwxrwxr-x 487 xxxxx xxxxx 20480 3æ 22 23:56 node_modules/ -rw-rw-r-- 1 xxxxx xxxxx 584349 3æ 22 23:56 package-lock.json -rw-rw-r-- 1 xxxxx xxxxx 2011 3æ 22 23:55 package.json drwxrwxr-x 2 xxxxx xxxxx 4096 3æ 22 23:55 src/ drwxrwxr-x 2 xxxxx xxxxx 4096 3æ 22 23:55 test/ -rw-rw-r-- 1 xxxxx xxxxx 97 3æ 22 23:55 tsconfig.build.json -rw-rw-r-- 1 xxxxx xxxxx 546 3æ 22 23:55 tsconfig.json
ãœãŒã¹ã³ãŒãããã³ãã¹ãã³ãŒããé 眮ãããŠãããã£ã¬ã¯ããªã
$ tree src test src âââ app.controller.spec.ts âââ app.controller.ts âââ app.module.ts âââ app.service.ts âââ main.ts test âââ app.e2e-spec.ts âââ jest-e2e.json 0 directories, 7 files
èšå®ãã¡ã€ã«ãèŠãŠã¿ãŸãããã
package.json
{ "name": "nestjs-getting-started", "version": "0.0.1", "description": "", "author": "", "private": true, "license": "UNLICENSED", "scripts": { "prebuild": "rimraf dist", "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", "@nestjs/platform-express": "^8.0.0", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.2.0" }, "devDependencies": { "@nestjs/cli": "^8.0.0", "@nestjs/schematics": "^8.0.0", "@nestjs/testing": "^8.0.0", "@types/express": "^4.17.13", "@types/jest": "27.4.1", "@types/node": "^16.0.0", "@types/supertest": "^2.0.11", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "eslint": "^8.0.1", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^4.0.0", "jest": "^27.2.5", "prettier": "^2.3.2", "source-map-support": "^0.5.20", "supertest": "^6.1.3", "ts-jest": "^27.0.3", "ts-loader": "^9.2.3", "ts-node": "^10.0.0", "tsconfig-paths": "^3.10.1", "typescript": "^4.3.5" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".*\\.spec\\.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "collectCoverageFrom": [ "**/*.(t|j)s" ], "coverageDirectory": "../coverage", "testEnvironment": "node" } }
nest-cli.json
{ "collection": "@nestjs/schematics", "sourceRoot": "src" }
tsconfig.json
{ "compilerOptions": { "module": "commonjs", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2017", "sourceMap": true, "outDir": "./dist", "baseUrl": "./", "incremental": true, "skipLibCheck": true, "strictNullChecks": false, "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false } }
tsconfig.build.json
{ "extends": "./tsconfig.json", "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] }
.prettierrc
{ "singleQuote": true, "trailingComma": "all" }
.eslintrc.js
module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.json', sourceType: 'module', }, plugins: ['@typescript-eslint/eslint-plugin'], extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', ], root: true, env: { node: true, jest: true, }, ignorePatterns: ['.eslintrc.js'], rules: { '@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', }, };
ãœãŒã¹ã³ãŒããèŠãŠã¿ãŸãããã
src
ãã£ã¬ã¯ããªå
ã®ãã¡ã€ã«ã®åœ¹å²ã¯ã以äžã«æžãããŠããŸãã
ã¢ããªã±ãŒã·ã§ã³ã®ãšã³ããªãŒãã€ã³ãã
src/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();
ã¢ããªã±ãŒã·ã§ã³ã®ã«ãŒãã¢ãžã¥ãŒã«ã
Modules | NestJS - A progressive Node.js framework
src/app.module.ts
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [], controllers: [AppController], providers: [AppService], }) export class AppModule {}
ãµãŒãã¹ã
src/app.service.ts
import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { getHello(): string { return 'Hello World!'; } }
ã³ã³ãããŒã©ãŒã
Controllers | NestJS - A progressive Node.js framework
src/app.controller.ts
import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() getHello(): string { return this.appService.getHello(); } }
ã³ã³ãããŒã©ãŒã®ãã¹ãã³ãŒãã
src/app.controller.spec.ts
import { Test, TestingModule } from '@nestjs/testing'; import { AppController } from './app.controller'; import { AppService } from './app.service'; describe('AppController', () => { let appController: AppController; beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], providers: [AppService], }).compile(); appController = app.get<AppController>(AppController); }); describe('root', () => { it('should return "Hello World!"', () => { expect(appController.getHello()).toBe('Hello World!'); }); }); });
ãã¡ãã¯ãŠããããã¹ãã§ããã
test
ãã£ã¬ã¯ããªã«ããã®ã¯ãE2Eãã¹ãã®ããã§ãã
test/app.e2e-spec.ts
import { Test, TestingModule } from '@nestjs/testing'; import { INestApplication } from '@nestjs/common'; import * as request from 'supertest'; import { AppModule } from './../src/app.module'; describe('AppController (e2e)', () => { let app: INestApplication; beforeEach(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }).compile(); app = moduleFixture.createNestApplication(); await app.init(); }); it('/ (GET)', () => { return request(app.getHttpServer()) .get('/') .expect(200) .expect('Hello World!'); }); });
E2Eãã¹ãã§ã®ãJestã®èšå®ãã¡ã€ã«ã
test/jest-e2e.json
{ "moduleFileExtensions": ["js", "json", "ts"], "rootDir": ".", "testEnvironment": "node", "testRegex": ".e2e-spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" } }
åãããŠã¿ã
ãšããããããã®ç¶æ ã§èµ·åããŠã¿ãŸããããã
$ npm start
èµ·åããŸããã
> nestjs-getting-started@0.0.1 start > nest start [Nest] 61845 - 2022/03/23 0:22:49 LOG [NestFactory] Starting Nest application... [Nest] 61845 - 2022/03/23 0:22:49 LOG [InstanceLoader] AppModule dependencies initialized +104ms [Nest] 61845 - 2022/03/23 0:22:49 LOG [RoutesResolver] AppController {/}: +5ms [Nest] 61845 - 2022/03/23 0:22:49 LOG [RouterExplorer] Mapped {/, GET} route +3ms [Nest] 61845 - 2022/03/23 0:22:49 LOG [NestApplication] Nest application successfully started +2ms
ãã¡ãã«åŸã£ãŠãã¢ã¯ã»ã¹ããŠã¿ãŸãã
First steps / Running the application
$ curl localhost:3000 Hello World!
src/app.module.ts
ã§ã¯ã3000ããŒãããªãã¹ã³ããããã«ãªã£ãŠããŸããã
ã«ãŒãã£ã³ã°ã¯ããã°ã«å®çŸ©ãåºåãããŠããŸãããã
[Nest] 61845 - 2022/03/23 0:22:49 LOG [RoutesResolver] AppController {/}: +5ms [Nest] 61845 - 2022/03/23 0:22:49 LOG [RouterExplorer] Mapped {/, GET} route +3ms
ã³ã³ããŒããŒã©ãŒã®URLã¯ãã©ã®ããã«èŠããããã®ã§ããããïŒ
ã«ãŒãã£ã³ã°ã®èª¬æãèŠããšã@Controller
ãã³ã¬ãŒã¿ãŒã§ãã¹ãæå®ããããã§ããã
ä»åã¯ç¹ã«ãªã«ãæå®ãããŠããªãã£ãã®ã§ããã®ãŸãŸ/
ïŒã«ãŒãïŒã«ãããã³ã°ãããŸããããšã
@Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() getHello(): string { return this.appService.getHello(); } }
@Get
ãã³ã¬ãŒã¿ãŒã«ããã¹ãæå®ã§ããããã§ãã
ã³ã³ãããŒã©ãŒã远å ããŠã¿ã
ããã«ãã³ã³ãããŒã©ãŒã远å ããŠã¿ãŸãããã
$ npx nest g controller hello
g
ã¯ãgenerate
ãµãã³ãã³ãã®ç瞮圢ã§ãã
å®è¡çµæã2ã€ã®ãã¡ã€ã«ãäœæãããsrc/app.module.ts
ãæŽæ°ãããããã§ãã
CREATE src/hello/hello.controller.spec.ts (485 bytes) CREATE src/hello/hello.controller.ts (99 bytes) UPDATE src/app.module.ts (326 bytes)
çæããããã¡ã€ã«ãèŠãŠã¿ãŸãããã
sc/hello/hello.controller.ts
import { Controller } from '@nestjs/common'; @Controller('hello') export class HelloController {}
src/hello/hello.controller.spec.ts
import { Test, TestingModule } from '@nestjs/testing'; import { HelloController } from './hello.controller'; describe('HelloController', () => { let controller: HelloController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [HelloController], }).compile(); controller = module.get<HelloController>(HelloController); }); it('should be defined', () => { expect(controller).toBeDefined(); }); });
src/app.module.ts
ã¯ã以äžã®ããã«çæããHelloController
ã远å ãããŠããŸãã
src/app.module.ts
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { HelloController } from './hello/hello.controller'; @Module({ imports: [], controllers: [AppController, HelloController], providers: [AppService], }) export class AppModule {}
nest generate
ã§ã¯ããã®ããã«ãã¡ã€ã«ã®è¿œå ã倿Žãã§ããããã§ããã©ã®ãããªçš®é¡ã®ãã¡ã€ã«ãçæã§ãããã¯ã以äžã®ããŒãžã«
äžèŠ§è¡šããããŸãã
CLI command reference / nest generate
çæãããã³ã³ãããŒã©ãŒããã¡ãã£ãšä¿®æ£ããŠã¿ãŸãããã
src/hello/hello.controller.ts
import { Body, Controller, Get, Post, Query } from '@nestjs/common'; @Controller('hello') export class HelloController { @Get('get') async getMessage(@Query('message') message: string): Promise<string> { return `Hello ${message}!!`; } @Post('post') async postMessage(@Body() request: any): Promise<any> { return { responseMessage: `Hello ${request.message}!!`, }; } }
@Query
ã@Body
ã®äœ¿ãæ¹ãasync
ã«ã€ããŠã¯ã以äžããããåèã«ã
éçºäžã¯ãstart:dev
ã§ã¢ããªã±ãŒã·ã§ã³ãèµ·åããŠãããšããœãŒã¹ã³ãŒãã®ä¿®æ£ã«åãããŠãªããŒãããŠããããã§ãã
$ npm run start:dev
åäœç¢ºèªã
$ curl localhost:3000/hello/get?message=NestJS Hello NestJS!! $ curl -XPOST -H 'Content-Type: application/json' localhost:3000/hello/post -d '{"message": "NestJS" }' {"responseMessage":"Hello NestJS!!"}
OKã§ããã
ãã¹ãã³ãŒããä¿®æ£ããŠãããŸãããã
src/hello/hello.controller.spec.ts
import { Test, TestingModule } from '@nestjs/testing'; import { HelloController } from './hello.controller'; describe('HelloController', () => { let controller: HelloController; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [HelloController], }).compile(); controller = module.get<HelloController>(HelloController); }); it('should return "Hello NestJS!!"', async () => { expect(await controller.getMessage('NestJS')).toBe('Hello NestJS!!'); }); it('should return "Hello NestJS!!" object', async () => { expect(await controller.postMessage({ message: 'NestJS' })).toEqual({ responseMessage: 'Hello NestJS!!', }); }); });
ä»åã¯ãããŸã§ã«ããŠãããŸããããã
ãŸãšã
NestJSãåããŠäœ¿ã£ãŠã¿ãŸããã
CLIã§ã»ããã¢ããã§ããŠç°¡åã«å§ããããã®ãšãããã¥ã¡ã³ãã«æ²¿ã£ãŠé²ããŠãããšåºæ¬çãªããšã¯ããã«ã§ããããªæ°ãããŸããã
Node.jsã§ãµãŒããŒãµã€ãã¢ããªã±ãŒã·ã§ã³ãæžãæã«ã¯ãExpressãããNestJSããªããšã¡ãã£ãšæãå§ããŸããã