[번역] Nest 공식문서 - 2. 개요 : 첫 단계

SuJeong·2022년 6월 22일
1
post-thumbnail

공식 문서 바로가기

First steps 첫 단계

In this set of articles, you'll learn the core fundamentals of Nest.

이번 포스트에서는, Nest의 핵심적인 기본을 배울 것이다.

To get familiar / with the essential building blocks of Nest applications, / we'll build a basic CRUD application with features / that cover a lot of ground at an introductory level.

필수적인 Nest 어플리케이션의 구성 요소에 익숙해지기 위해, 많은 입문 수준의 기능을 포함하는 특징을 가진 기본적인 CRUD 어플리케이션을 빌드할 것이다.

Language 언어

We're in love with TypeScript, / but above all - we love Node.js.

우리는 TypeScript를 사랑하지만, 무엇보다 Node.js를 사랑한다.

That's why Nest is compatible / with both TypeScript and pure JavaScript.

이것이 Nest가 TypeScript와 순수 JavaScript 모두 호환되는 이유이다.

Nest takes advantage of the latest language features, / so to use it with vanilla JavaScript / we need a Babel compiler.

Nest는 최신의 언어를 이용하기 때문에, 기능을 *바벨 컴파일러가 필요한 바닐라 JavaScript를 사용한다.

*babel compiler : 구버전 코드를 호환 가능한 JavaScript 버전으로 변환하는데 사용되는 JavaScript 컴파일러

We'll mostly use TypeScript / in the examples we provide, / but you can always switch the code snippets / to vanilla JavaScript syntax (simply click to toggle the language button / in the upper right hand corner of each snippet).

여기서 제공하는 예시는 TypeScript를 사용할 것이지만, 언제든 바닐라 JavaScript 문법으로 바꿀 수 있다(간단하게 각 코드의 오른쪽 상단에 있는 언어 버튼을 클릭하여 전환).

Prerequisites 전제 조건

Please make sure that Node.js (>= 10.13.0, except for v13) is installed / on your operating system.

당신의 운영체제에 Node.js(10.13.0 이상, v13 제외)가 설치되어 있어야한다.

Setup 설치

Setting up a new project / is quite simple with the Nest CLI.

새로운 프로젝트를 설정하는 것은 꽤 간단히 Nest CLI를 사용한다.

With npm installed, / you can create a new Nest project / with the following commands in your OS terminal:

npm을 설치하고, 운영체제 터미널에서 다음의 명령어로 새로운 Nest 프로젝트를 만들 수 있다.

$ npm i -g @nestjs/cli
$ nest new project-name

The project-name directory / will be created, node modules and a few other boilerplate files / will be installed, / and a src/ directory will be created and populated / with several core files.

프로젝트 이름 폴더와 설치된 node modules, 몇가지 다른 뼈대 파일, src/ 폴더는 몇가지 핵심 파일과 함께 만들어진다.

src
ㄴapp.controller.spec.ts
ㄴapp.controller.ts
ㄴapp.module.ts
ㄴapp.service.ts
ㄴmain.ts

Here's a brief overview of those core files:

핵심 파일에 대한 짧은 개요이다.

파일설명
app.controller.tsA basic controller with a single route.단일 경로의 기본 컨트롤러
app.controller.spec.tsThe unit tests for the controller.컨트롤러의 단위 테스트
app.module.tsThe root module of the application.어플리케이션의 루트 모듈
app.service.tsA basic service with a single method.단일 속성의 기본 서비스
main.tsThe entry file of the application / which uses the core function NestFactory / to create a Nest application instance.NestFactory 핵심 함수를 사용하는 어플리케이션의 항목 파일은 Nest 어플리케이션 인스턴스를 생성함

The main.ts includes an async function, which will bootstrap our application:

main.ts는 어플리케이션을 *부트스트랩하는 비동기 함수를 포함한다.

*bootstrap : 일체의 외부 도움없이 스스로 진행함(제어)

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

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

To create a Nest application instance, / we use the core NestFactory class.

Nest 어플리케이션 인스턴스를 생성하기 위해, core NestFactory 클래스를 사용한다.

NestFactory exposes a few static methods / that allow creating an application instance.

NestFactory는 어플리케이션 인스턴스를 생성하는 몇가지 정적인 메소드를 제공한다.

The create() method returns an application object, / which fulfills the INestApplication interface.

create() 메소드는 INestApplication 인터페이스를 수행하는 어플리케이션 객체를 반환한다.

This object provides a set of methods / which are described in the coming chapters.

이 객체는 다음 챕터의 메서드 집합을 제공한다.

In the main.ts example above, / we simply start up our HTTP listener, / which lets the application await inbound HTTP requests.

위의 예시의 main.ts에서, 단순하게 어플리케이션이 인바운드 HTTP 요청을 기다리는 HTTP 수신기를 시작하면 된다.

Note / that a project scaffolded with the Nest CLI / creates an initial project structure / that encourages developers to follow the convention of keeping each module / in its own dedicated directory.

Nest CLI로 스캐폴딩된 프로젝트는 개발자가 따르도록 권유하는 각각의 모듈을 자체 전용 디렉토리에 보관하는 컨밴션인 초기 프로젝트 구조를 생성한다.

Hint: By default, if any error happens while creating the application / your app will exit with the code 1. / If you want to make it throw an error / instead disable the option abortOnError (e.g., NestFactory.create(AppModule, { abortOnError: false })).

기본적으로 어플리케이션을 생성할 때 어떤 오류가 발생하면, 앱은 code 1로 종료할 것이다. 만약 오류 발생을 원한다면, 대신 abortOnError 옵션을 비활성화한다.

(예 : NestFactory.create(AppModule, { abortOnError: false }))

Platform 플랫폼

Nest aims to be a platform-agnostic framework.

Nest는 플랫폼에 구애받지 않는 프레임워크를 목표로 한다.

Platform independence makes it possible to create reusable logical parts / that developers can take advantage of across several different types of applications.

플랫폼 독립성은 개발자들이 몇가지 다른 유형의 어플리케이션을 재사용가능한 논리적 부분을 만드는 것이 가능하다.

Technically, Nest is able to work with any Node HTTP framework once an adapter is created.

기술적으로, Nest는 어댑터가 생성되면 Node HTTP 프레임워크로 작동할 수 있다.

There are two HTTP platforms supported out-of-the-box: express and fastify.

즉시 지원되는 두 개의 HTTP 플랫폼(express와 fastify)이 있다.

You can choose the one that best suits your needs.

필요에 가장 어울리는 하나를 선택할 수 있다.

이름설명번역
platform-expressExpress is a well-known minimalist web framework for node. It's a *battle tested, production-ready library with lots of resources implemented by the community. The @nestjs/platform-express package is used by default. Many users are well served with Express, and need take no action to enable it.Express는 node의 유명한 미니멀리스트 웹 프레임워크이다. 커뮤니티로부터 구현된 많은 리소스로 전투 테스트를 거친 제품 시작 라이브러리이다. 기본적으로 @nestjs/platform-express 패키지가 사용된다. 많은 유저들은 express로 서버를 잘 만들고, 사용하기 위해 조치가 필요하지 않다.
platform-fastifyFastify is a high performance and low overhead framework / highly focused on providing maximum efficiency and speed. Read how to use it here.Fastify는 고도의 최대 효율과 속도를 제공하는것에 초점을 맞춘 높은 성능과 낮은 오버헤드를 가진 프레임워크이다. 사용 방법은 여기로 이동.

*battle tested library : 버그가 거의 또는 전혀 없고 프로덕션에서 자신있게 사용할 수 있는 라이브러리

Whichever platform is used, / it exposes its own application interface.

어떤 플랫폼이 사용되건, 그 자체의 어플리케이션 인터페이스를 보여준다.

These are seen respectively as NestExpressApplication and NestFastifyApplication.

각각 NestExpressApplication와 NestFastifyApplication로서 보여진다.

When you pass a type to the NestFactory.create() method, as in the example below / the app object will have methods available exclusively for that specific platform.

아래 예시와 같이 NestFactory.create() 메소드에 한 타입을 보낼때, 앱 객체는 특정한 플랫폼에서 독점적으로 사용가능한 메소드를 가진다.

Note, however, you don't need to specify a type / unless you actually want to access the underlying platform API.

그러나 실제로 기본 플랫폼 API에 접근하길 원하는게 아닌 이상, 타입을 지정할 필요가 없다.

const app = await NestFactory.create<NestExpressApplication>(AppModule);

Running the application 어플리케이션 실행

Once the installation process is complete, you can run the following command at your OS command prompt / to start the application listening for inbound HTTP requests:

프로세스 설치가 완료하면, 인바운드 HTTP 요청을 수신하는 어플리케이션을 시작하는 다음 명령어를 OS 명령 프롬포트에서 실행할 수 있다.

$ npm run start

This command starts the app with the HTTP server / listening on the port defined in the src/main.ts file.

이 명령어는 src/main.ts 파일에 정의된 포트로 수신 대기하는 HTTP 서버로 앱을 실행한다.

Once the application is running, open your browser and navigate to http://localhost:3000/.

어플리케이션이 실행되면, 브라우저를 열고 http://localhost:3000/로 이동한다.

You should see the Hello World! message.

Hello World! 메시지가 보인다.

To watch for changes in your files, you can run the following command to start the application:

파일의 변경을 보기 위해서, 다음 명령어로 어플리케이션을 실행할 수 있다. :

$ npm run start:dev

This command will watch your files, / automatically recompiling and reloading the server.

이 명령어는 자동으로 재 컴파일되고 서버를 재시작하면서 파일을 감시한다.

reference

https://velog.io/@kwonh/Babel-%EA%B8%B0%EB%B3%B8-%EC%82%AC%EC%9A%A9%EB%B2%95%EA%B3%BC-%EC%BB%B4%ED%8C%8C%EC%9D%BC%EA%B3%BC%EC%A0%95

https://www.wisewiredbooks.com/term-dict/common/bootstrap.html

https://www.educative.io/answers/what-is-a-battle-tested-library

profile
backend developer / 꾸준히 배우고 적용하는걸 좋아합니다!

0개의 댓글