W14D2 Nest.js Introduction

Jin Bae·2023년 2월 14일
0

스파르타코딩클럽

목록 보기
31/35

Cons of Express.js

To require other types of data, you need to install other middleware, such as body-parser (express.json() module can be used after Express v4.16), cookie-parser (for logging in/out), cors (Cross-origin resource sharing), etc.

These middlewares need to be studied and used to create the service the developer desires.

Layered Architecture Pattern review

The layered architecture pattern is an architecture created from the SRP principle (Single-Responsibility Principle, or 단일 책임 원칙, S from the SOLID principles) from the SOLID principles.

Single-responsibility Principle (SRP) states:
A class should have one and only one reason to change, meaning that a class should have only one job.
-Reference

  • Presentation layer (프리젠테이션 계층):
    - Layer responsible for communicating with the client to understand its request and send a response
    • The API provided that communicates with the client is represented as the controller
    • Entrusts the request to the business layer and only performs the work of responding to the request
  • Business layer (비지니스 계층):
    - Layer that carries out the business logic for the request received from the presentation layer
    • Represented as the service layer
    • Communicates with the data layer
  • Data layer (데이터 계층):
    - The layer that access the actual database (RDBMS or NoSQL)
    • Represented as the repository layer

The layered architecture pattern has the following common characteristics.

  • Dependence (의존성)
    - Every layer has a dependency injection (DI) of its closest lower layer
  • Independency (독립성)
    - Every layer does not violate the other layer's role
    • Every layer has a clear role, making it easier to create functions and to test

🔔 With express, you need to clearly design a directory according to this layered architecture pattern and add more concepts if required by any changes or additions in the service requirements.

This may be fine for a senior developer but may be difficult for a junior developer.

Express is so well-known that there is a CLI tool such as express-generator or other developers create an open-source boilerplate (boilerplate: sections of code that are repeated in multiple places with little to no variation). However, these boilerplates aren't officially recognized and must be decided to be used by the developer.

Nest.js

Nest.js is a web framework based on Typescript and commands. Because it's based on Typescript, it can strictly check types and avoid various exceptions in advance.

To create a controller named 'Posts' type the following command:

nest g co posts

g: generate
co: controller
posts: name

A big pro of Nest.js is that it can exactly create various components needed for a web servers, such as controllers, service, middlewares, interceptors, etc. Nest.js will systematically create this components, preventing developers from making mistakes. It also automatically creates the project directory.

This helps developers to only focus on creating the key logic for the web server.

0개의 댓글