Node.js

Peter Oh·2021년 1월 16일
0

Node.js

목록 보기
4/4

What is Node.js

비동기 이벤트 기반(event-driven) JavaScript runtime환경

Runtime 환경이란?

Runtime describes software/instructions that are executed while your program is running, especially those instructions that you did not write explicitly, but are necessary for the proper execution of your code.

Low-level languages like C have very small (if any) runtime. More complex languages like Objective-C, which allows for dynamic message passing, have a much more extensive runtime.

You are correct that runtime code is library code, but library code is a more general term, describing the code produced by any library. Runtime code is specifically the code required to implement the features of the language itself.

비동기(asynchronous)

  • 프로그램의 기능 혹은 함수 들이 서로 방해(blocking) 하지 않고 동시에 일어날 수 있음
  • 역할의 분리가 핵심
  • 함수를 비동기로 실행하기 위해서 javascript에서는 promise라는 개념을 이용
  • 함수는 Promise() 객체를 return(반환)해줘야 한다
  • promise 는 인자로,
    - resolve, reject 두가지를 객체로 받는다
    • Promise((resolve, reject) =>
  • Single Thread 의 역할은 카페 종업원
    - Internal C++ thread pool은 각각의

callback - > 함수의 인자로 함수를 사용하는 것

# es5 까지의 문법
# callback 지옥의 예
fetch('BACKEND_API', (res) => {
  fetch('BACKEND_API', (res) => {
    fetch('BACKEND_API', (res) => {    
      
    })
  })
})  

# 가독성과 유지보수 차원에서 어려운 callback지옥
  => callback(함수)으로 처리하던 비동기 작업을 handling하기 위해서 promise라는 개념이 생김!

JavaScript 런타임(runtime) 환경

Node.js에서 비동기로 처리해 줘야 할 대표적인 작업!

  • DB에서 데이터를 가지고 오는 작업
  • 암호화 작업 - 연산이 많기 때문에!

js에서 this는 python에서 self와 같은 역할이다

MVC (Model View Controller)
View -(Request)-> Controller
View <-(Response)- Contoller

ex)
django-views.py - 컨트롤러 역할, 유저의 요청을 제어하는 곳

urls.py 가 따로 존재하는 이유?
API의 module화 하기 위해서! -> 코드간의 의존성을 낮춤

Route의 역할

  • 길!
  • url/endpoint
  • 함수를 각각 맞는 url에 mapping해주기 위해 존재

About Dependency

Backend
Route
Controller
Service
Model

  • 각각의 layer는 바로 아래(하위) layer에만 의존함!!
    -> ex) mysql에서 mongodb로 db를 바꾸고 싶으면, service 쪽만 코드를 수정해주면 된다!!

Callback 함수

JS에서 함수는 일급객체(first-class-citizen)

  • 프로그래밍 언어에서 type을 전달, 반환 및 할당 할 수 있는 경우 해당 type을 1급 객체로 간주
  • Javascript에서 함수를 반환할 수 있을 뿐만 아니라 함수를 받을 수 있는 함수를 만들수
  • 하나 이상의 함수를 인수로 받거나 함수를 반환하는 고차 함수
    - 일급객체(first-class-citizen)
    • 변수나 데이터 구조안에 담을 수 있다.
    • 파라미터로 전달 할 수 있다.
    • 리턴 값으로 사용할 수 있다.
profile
def backend_engineer():

0개의 댓글