{"componentChunkName":"component---src-templates-blog-post-js","path":"/nodejs/book1_01/","result":{"data":{"site":{"siteMetadata":{"title":"Progress Not Perfection","author":"Sunmin","siteUrl":"https://sunmin.netlify.com","comment":{"disqusShortName":"","utterances":"Sunmin0520/blog"}}},"markdownRemark":{"id":"a9e9b1fb-7f61-54bf-815e-bd4aaf7f62a8","excerpt":"1. 핵심 개념 1. 서버 Node.js: 크롬 V8 자바스크립트 엔진으로 빌드된 자바스크립트 런타임 노드를 통해 다양한 JS 애플리케이션 실행 가능하지만, 서버 애플리케이션을 실행하는데에 가장 많이 사용 2. JS 런타임 노드 내부 구조: Node.js core library & Node.js Bindings & V8 & libUV libUV: 이벤트 기반, 논블로킹 I/O 모델 구현…","html":"<p>\n<h2 id=\"1-핵심-개념\" style=\"position:relative;\"><a href=\"#1-%ED%95%B5%EC%8B%AC-%EA%B0%9C%EB%85%90\" aria-label=\"1 핵심 개념 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. 핵심 개념</h2>\n<h3 id=\"1-서버\" style=\"position:relative;\"><a href=\"#1-%EC%84%9C%EB%B2%84\" aria-label=\"1 서버 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. 서버</h3>\n<ul>\n<li>Node.js: 크롬 V8 자바스크립트 엔진으로 빌드된 자바스크립트 런타임</li>\n<li>노드를 통해 다양한 JS 애플리케이션 실행 가능하지만, 서버 애플리케이션을 실행하는데에 가장 많이 사용</li>\n</ul>\n<h3 id=\"2-js-런타임\" style=\"position:relative;\"><a href=\"#2-js-%EB%9F%B0%ED%83%80%EC%9E%84\" aria-label=\"2 js 런타임 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. JS 런타임</h3>\n<ul>\n<li>\n<p>노드 내부 구조: <strong>Node.js core library</strong> &#x26; <strong>Node.js Bindings</strong> &#x26; <strong>V8</strong> &#x26; libUV</p>\n<ul>\n<li>libUV: 이벤트 기반, 논블로킹 I/O 모델 구현</li>\n</ul>\n</li>\n</ul>\n<h3 id=\"3-이벤트-기반\" style=\"position:relative;\"><a href=\"#3-%EC%9D%B4%EB%B2%A4%ED%8A%B8-%EA%B8%B0%EB%B0%98\" aria-label=\"3 이벤트 기반 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. 이벤트 기반</h3>\n<ul>\n<li>\n<p><strong>이벤트기반</strong>: 이벤트가 발생할 때 미리 지정해둔 작업을 수행하는 방식 (이벤트 발생시 무엇을 할지 이벤트 리스너에 콜백을 등록)</p>\n<ul>\n<li>예: 클릭 이벤트 리스너에 경고창 띄우는 콜백 등록 ➡ 클릭 이벤트 발생 ➡ 콜백함수 실행되어 경고창 ➡ 처리해야할 이벤트 없으면 대기</li>\n</ul>\n</li>\n<li>\n<p><strong>이벤트 루프</strong>: 이벤트 발생시 호출할 콜백 함수를 관리 &#x26; 여러 이벤트 동시에 발생시 콜백의 호출 순서 결정. 노드가 종료될 때까지 이벤트 처리를 위한 작업 반복</p>\n<ul>\n<li>콜스택이 비면 이벤트루프가 태스크 큐의 콜백을 꺼내 콜스택으로 올림. 만약 콜스택에 함수가 많으면 setTimeout으로 지정한 delay가 지나도 콜백이 실행되지 않을 수도 있다.</li>\n</ul>\n</li>\n</ul>\n<h3 id=\"4-논블로킹-io\" style=\"position:relative;\"><a href=\"#4-%EB%85%BC%EB%B8%94%EB%A1%9C%ED%82%B9-io\" aria-label=\"4 논블로킹 io permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. 논블로킹 I/O</h3>\n<ul>\n<li>I/O의 예: 파일시스템 접근(파일 읽기, 쓰기, 폴더 만들기 등), 네트워크 통한 요청 등</li>\n<li>\n<p><strong>노드는 libUV 사용해서 I/O 작업을 논블로킹으로 처리</strong>(이전 작업 완료될 때까지 기다리지 않고 다음 작업 수행)</p>\n<ul>\n<li>콜스택, 백그라운드, 태스크 큐 중 백그라운드에 I/O 작업을 넘겨서 동시에 처리 => 동시에 처리할 수 있는 작업들은 최대한 묶어서 백그라운드로 넘겨야 시간 절약 => setTimeout, setImmediate 등 이용</li>\n</ul>\n</li>\n<li>I/O 작업이 없어도, 논블로킹 통해 실행 순서 바꾸어서 간단한 작업들이 대기해야하는 상황 방지 가능  </li>\n</ul>\n<h3 id=\"5-싱글스레드\" style=\"position:relative;\"><a href=\"#5-%EC%8B%B1%EA%B8%80%EC%8A%A4%EB%A0%88%EB%93%9C\" aria-label=\"5 싱글스레드 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. 싱글스레드</h3>\n<ul>\n<li>\n<p><strong>프로세스</strong>: 운영체제에서 할당하는 작업의 단위/ 노드나 웹 브라우저 등의 프로그램은 개별적인 프로세서</p>\n<ul>\n<li>프로세서 간에는 자원 공유 X</li>\n<li>멀티프로세싱: I/O 요청이 많을 때에 사용</li>\n</ul>\n</li>\n<li>\n<p><strong>스레드</strong>: 프로세스 내에서 실행되는 흐름의 단위/ 프로세스는 스레드를 여러개 생성해서 여러 작업 동시에 처리 가능</p>\n<ul>\n<li>같은 주소의 메모리에 접근 ➡ 자원 공유 O</li>\n<li>멀티 스레딩: CPU 작업이 많을 때 사용</li>\n</ul>\n</li>\n<li>\n<p>노드 실행 ➡ 1개의 프로세스와 그 프로세서 내부의 여러 개의 스레드 생성/ 이 중에서 내가 직접 제어할 수 있는 스레드는 하나 뿐 => 노드가 싱글스레드라고 여겨짐</p>\n<ul>\n<li>노드가 싱글 스레드로 동작하지 않는 경우</li>\n<li>Thread pool: 노드가 특정 동작 수행시 멀티스레드 사용(예: 암호화, 파일 입출력, 압축 등)</li>\n<li>Worker Thread: 내가 직접 멀티스레드를 다룰 수 있다.(예: CPU 작업이 많은 경우)</li>\n</ul>\n</li>\n</ul>\n<h2 id=\"2-서버로서의-노드\" style=\"position:relative;\"><a href=\"#2-%EC%84%9C%EB%B2%84%EB%A1%9C%EC%84%9C%EC%9D%98-%EB%85%B8%EB%93%9C\" aria-label=\"2 서버로서의 노드 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. 서버로서의 노드</h2>\n<ul>\n<li>스레드 하나가 많은 I/O 처리 가능. 하지만 싱글스레드이기에 CPU 부하가 큰 작업에는 부적합</li>\n<li>\n<p>노드는 <strong>크기 작은 많은 수의 데이터 주고받을 떄 적합</strong></p>\n<ul>\n<li>예: 네트워크, DB, 디스크 작업 등의 I/O (채팅, 주식차트, JSON 데이터 제공하는 API 서버)</li>\n<li>워커 스레드로 멀티스레드 가능해졌지만, 다른 언어에 비해 느리고 그 분배가 어렵다. ➡ 비디오, 대규모 데이터처리에는 부적합</li>\n</ul>\n</li>\n</ul>\n<h2 id=\"3-노드가-서버-외의-기능으로-사용된-예\" style=\"position:relative;\"><a href=\"#3-%EB%85%B8%EB%93%9C%EA%B0%80-%EC%84%9C%EB%B2%84-%EC%99%B8%EC%9D%98-%EA%B8%B0%EB%8A%A5%EC%9C%BC%EB%A1%9C-%EC%82%AC%EC%9A%A9%EB%90%9C-%EC%98%88\" aria-label=\"3 노드가 서버 외의 기능으로 사용된 예 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. 노드가 서버 외의 기능으로 사용된 예</h2>\n<ul>\n<li>React, Electron, Slack, Discord 등의 애플리케이션</li>\n</ul>\n</br>\n</br>\n<p><strong>출처</strong></p>\n<ul>\n<li>조현영, Node.js 교과서(2021, 길벗)</li>\n</ul>\n</p>","frontmatter":{"title":"[Node.js] [Node.js 교과서] 1. 노드 시작하기","date":"March 23, 2021"}}},"pageContext":{"slug":"/nodejs/book1_01/","previous":{"fields":{"slug":"/CS/compile_transpile_paradigm/"},"frontmatter":{"title":"[CS] compile, transpile, program paradigm","category":"cs","draft":false}},"next":{"fields":{"slug":"/git/git_flow/"},"frontmatter":{"title":"[git] git flow","category":"git","draft":false}}}},"staticQueryHashes":["3128451518","521680639"]}