티스토리 뷰

반응형

[nestjs] 1. testController / testService 생성 및 helloworld

 

0. nest 기본 생성 규칙

nest g(generate) [name] or [alias] [keyword]

 

1. testController / testService 생성

$ nest g co test
$ nest g s test

 

2. testService에 getTest 함수 추가

import { Injectable } from '@nestjs/common';

@Injectable()
export class TestService {
  getTest(): string {
    return 'test';
  }
}

 

3. testController testService 등록 및 @Get() 추가

import { Controller, Get } from '@nestjs/common';
import { TestService } from './test.service';

@Controller('test')
export class TestController {
  constructor(private readonly testService: TestService) {}

  @Get()
  getTest(): string {
    return this.testService.getTest();
  }
}

추가 설명: contructor에 readonly 등록함으로써 Inject로 추가

 

4. 작동 확인

$ npm run start

 

5. github commit 및 최신 소스 clone

[github commit]

https://github.com/gofogo2/nest-js-api/commit/7c3da3d6de627a79559068460ecaafa84008056a

 

update testController / testService · gofogo2/nest-js-api@7c3da3d

Permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. Showing 5 changed files with 31 additions and 3 deletions. +6 −0 .eslintrc.js +4 −2 src/app.module.ts +1 −1 src/app.service.ts

github.com

 

[github 최신소스 clone]

https://github.com/gofogo2/nest-js-api.git

 

GitHub - gofogo2/nest-js-api

Contribute to gofogo2/nest-js-api development by creating an account on GitHub.

github.com

 

 

#nestjs #typescript #javascript #js #ts #api #controller #service

반응형
댓글
반응형