1. Node.js๋?
1.1 ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๋ ค๋ฉด?
์ปดํจํฐ์์ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ ์ ์๋์? ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ๋ธ๋ผ์ฐ์ ์์๋ ์คํํ ์ ์์ต๋๋ค. ํ์ง๋ง ๋ธ๋ผ์ฐ์ ๊ฐ ์๋ค๋ฉด? ์๋ฐ์คํฌ๋ฆฝํธ ์คํํ ์ ์์ต๋๋ค.
cf. ๋ธ๋ผ์ฐ์ ์๋ ์ด๋ฏธ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ด ์๊ธฐ์ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ ์ ์์ต๋๋ค.
A JavaScript engine is a software component that executes JavaScript code. The first JavaScript engines were mere interpreters, but all relevant modern engines use just-in-time compilation for improved performance. JavaScript engines are typically developed by web browser vendors, and every major browser has one.
JavaScript ์์ง์ JavaScript ์ฝ๋๋ฅผ ์คํํ๋ ์ํํธ์จ์ด ๊ตฌ์ฑ ์์์ ๋๋ค. ์ต์ด์ JavaScript ์์ง์ ๋จ์ํ interpreter ์์ง๋ง, ๋ชจ๋ ๊ด๋ จ ์ต์ ์์ง์ ์ฑ๋ฅ ํฅ์์ ์ํด Just-In-Time ์ปดํ์ผ์ ์ฌ์ฉํฉ๋๋ค. JavaScript ์์ง์ ์ผ๋ฐ์ ์ผ๋ก ์น ๋ธ๋ผ์ฐ์ ๊ณต๊ธ์ ์ฒด์์ ๊ฐ๋ฐํ๋ฉฐ ๋ชจ๋ ์ฃผ์ ๋ธ๋ผ์ฐ์ ์ ํ๋๊ฐ ์์ต๋๋ค.
1.2 ์ปดํจํฐ๊ฐ ์์ค ์ฝ๋๋ฅผ ์ดํดํ๋ ค๋ฉด?
์ปดํจํฐ ํ๋ก์ธ์๋ ์ค์ง 0๊ณผ 1๋ง ์ดํดํ ์ ์๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๊ฐ ์์ฑํ ์์ค ์ฝ๋๋ฅผ ๋จธ์ ์ฝ๋๋ก ๋ณํํด ์ค์ผ ํฉ๋๋ค.
์ด ๋ณํ ๊ณผ์ ์ ์ปดํ์ผ๊ณผ ์ธํฐํ๋ฆฌํฐ๋ฅผ ํตํด ์ผ์ด๋ฉ๋๋ค.
Interpreter: ์คํํ ๋๋ง๋ค ํ์ค ์ฉ ๋ฒ์ญCompiler: ์ฌ๋์ด ์์ฑํ ์ฝ๋๋ฅผ ํ๋ฒ์ ๊ธฐ๊ณ์ด๋ก ๋ณํ
1.3 JIT Compiliation (Just In Time)
์ด๊ธฐ์ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ๊ทธ์ Interpreter ์๋ค๋ฉด ํ์ฌ๋ JIT Compiliation์ ์ด์ฉํ๊ณ ์์ต๋๋ค. ์๋ํ๋ฉด Interpreter ์ธ์ด๋ ์ปดํ์ผ ๋๋ ๋ค๋ฅธ ์ธ์ด๋ณด๋ค ํ์ค ํ์ค ํด์ํ๊ณ ์คํํ๊ธฐ ๋๋ฌธ์ ๋งค์ฐ ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ ๋๋ค. ํ์ง๋ง ํ์ฌ ์น์์๋ ์ง๋ ๊ธฐ๋ฅ๋ฑ ๊ต์ฅํ Heavyํ ๊ธฐ๋ฅ๋ค๋ ์ํํ๊ณ ์๊ธฐ์, ํ์ฌ๋ ๋ ๋์ ํผํฌ๋จผ์ค๋ฅผ ์ํด์ JIT ์ปดํ์ผ์ ์ด์ฉํ๊ณ ์์ต๋๋ค.
In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations)[1] is a way of executing computer code that involves compilation during execution of a program (at run time) rather than before execution.
JIT compilation is a combination of the two traditional approaches to translation to machine codeโahead-of-time compilation (AOT), and interpretationโand combines some advantages and drawbacks of both.
-wikipedia
1.4 ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์์ง์ ์ข ๋ฅ
Google์ V8์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ JavaScript ์์ง์ ๋๋ค. SpiderMonkey๋ Firefox ๋ฐ ํด๋น ํฌํฌ์์ ์ฌ์ฉํ๊ธฐ ์ํด Mozilla์์ ๊ฐ๋ฐํ์ต๋๋ค. JavaScriptCore๋ Apple์ Safari ๋ธ๋ผ์ฐ์ ์ฉ ์์ง์ ๋๋ค. ๋ค๋ฅธ WebKit ๊ธฐ๋ฐ ๋ธ๋ผ์ฐ์ ์์๋ ์ฌ์ฉํฉ๋๋ค. Chakra๋ Internet Explorer ๋ธ๋ผ์ฐ์ ์ ์์ง์ ๋๋ค. ๋ํ ์๋ Edge ๋ธ๋ผ์ฐ์ ์ฉ์ผ๋ก Microsoft์ ์ํด ๋ถ๊ธฐ๋์์ง๋ง, Edge๋ ๋์ค์ Chromium ๊ธฐ๋ฐ ๋ธ๋ผ์ฐ์ ๋ก ์ฌ๊ตฌ์ถ๋์ด ํ์ฌ V8์ ์ฌ์ฉํฉ๋๋ค.
| Chrome | V8 |
|---|---|
| Firefox | SpiderMonkey |
| Safari | JavascriptCore |
| Internet Explorer | Charkra |
2. Node.js ์ญ์ฌ
- 1995๋ , Netscape ๋ธ๋ผ์ฐ์ ๊ฐ JavaScript๋ฅผ ์๊ฐ
- 2008๋
, Web 2.0, Google์ด Chrome ๋ธ๋ผ์ฐ์ ๋ฐํ, Chrome ๋ธ๋ผ์ฐ์ ๋ JS์์ง์ผ๋ก V8์์ง์ ์ฌ์ฉ
- V8์์ง ๋๋ถ์ JS ์๋๊ฐ ๋นจ๋ผ์ง
- 2009๋
, Ryan Dahi๊ฐ Node.js ์ฐฝ์
- ๋ธ๋ผ์ฐ์ ์ธ๋ถ์์ V8์์ง์ ์ฌ์ฉํด JS๋ฅผ ์คํ์ํค๊ธฐ ์ํด์ ๋ง๋ฌ
- ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ๋ธ๋ผ์ฐ์ ๋ฐ์์ ์ด์ฉํ๋ ค๋ฉด? Node.js๋ฅผ ์ฌ์ฉํ๋ค.
3. Node.js Runtime
3.1 Runtime์ด๋?
๋ฐํ์: ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๊ฐ ์คํ๋๋ ํ๊ฒฝ์ ์๋ฏธ- Node.js๋ ํฌ๋กฌ ๋ฑ์ ์ฌ๋ฌ ๋ธ๋ผ์ฐ์ ๋ค์์ JS๊ฐ ๊ตฌ๋๋๊ธฐ์ Node.js๋ ๋ธ๋ผ์ฐ์ ๋ค๋ JS ๋ฐํ์์ด๋ค.
- Node.js๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ ํ๋ ์์ํฌ๋ ์๋ JS ๋ฐํ์์ด๋ค.
1Runtime Enviroment----------------------------------------23v8 ---> Node.js API ---> Node.js bindings ---> libuv45^ V8 : js์ฝ๋ ์คํ6^ libuv : db์ ๊ทผ, ํ์ผ ์ฝ๋ ๋ฑ v8์ด ๋ค๋ฃจ๋ ๊ฒ ์ด์ธ์๋ libuv๊ฐ ์ฒ๋ฆฌ7^ Node.js API : ํ์ผ ์์คํ ์ด๋, crypto ์ํธํ ์ฒ๋ฆฌ ๋ฑ์ API8: API๋ JS๋ก ์์ฑ๋ ๊ฒ๋ ์์ง๋ง, C++๋ C๋ก ๋์ด์์9----------------------------------------------------------
v8์ด ์ฝ๋๋ฅผ ํด์ํ๊ณ Node.js APIs๋ค ์ค ํ๋์ ํจ์๋ฅผ ํธ์ถํ๊ณ , Node.js ๋ฐ์ธ๋ฉ์ ํตํด์ libuv์ ์ํด์ ์ํ๋ ์์ ์ ์ฒ๋ฆฌํ๊ฒ ํฉ๋๋ค.
4. Node.js ์ค์น
1node --version # node ๋ฒ์ ํ์ธ2node # node ํ๊ฒฝ ์ง์3.exit # node ํ๊ฒฝ ๋น ์ ธ๋์ค๊ธฐ (ํน์ Ctrl + C๋ฅผ 2๋ฒ ํด์ฃผ๋ฉด ๋๋ค.)
4.1 REPL
1READ(์ฝ๊ธฐ), EVAL(ํ๊ฐ), PRINT(์ถ๋ ฅ)๋ฅผ ๋ฐ๋ณต
REPL(Read-Eval(evaluation)-Print Loop)- ์ฌ์ฉ์๊ฐ ํน์ ์ฝ๋๋ฅผ ์ ๋ ฅํ๋ฉด, ๊ทธ ์ฝ๋๋ฅผ ํ๊ฐํ๊ณ ์ฝ๋์ ์คํ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํด์ฃผ๋ ๊ฒ์ ๋ฐ๋ณตํด์ฃผ๋ ํ๊ฒฝ
- REPL ํ๊ฒฝ์ ๊ฐ๋จํ JS ์ฝ๋ ๋ฑ์ ์ฆ์์์ ์คํํด ๊ฒฐ๊ณผ๋ฅผ ํ์ธํด๋ณผ ๋ ์ฌ์ฉ
5. Browser API & Node.js API
- ๋ ๋ค ๋ชจ๋ ์ฌ์ฉํ๋ ๋ํ์ ์ธ API๊ฐ
console API๊ฐ ์๋ค. - cf. Node.js๋ V8์์ง์ ์ฌ์ฉํด ์ต์ ECMAScript ๊ธฐ์ค์ผ๋ก ๊ณ์ ์ ๋ฐ์ดํธํ๊ธฐ์ ์ต์ JS ๋ฌธ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค.
- ๋ธ๋ผ์ฐ์ API์ธ
window ๊ฐ์ฒด์ฌ์ฉ- ๋ธ๋ผ์ฐ์ ์์ ์ ๊ณตํ๋ ๊ฐ์ฒด๋ก Node.js repi ํ๊ฒฝ์์ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
- Node.js API์ธ
Process ๊ฐ์ฒด์ฌ์ฉ- node.js์์ ์ ๊ณตํด์ฃผ๋ ๊ฐ์ฒด์ผ๋ฟ, ๋ธ๋ผ์ฐ์ ์์ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
์์ ๋ค๋ฅธ API
| ๋ธ๋ผ์ฐ์ API | Node.js API |
|---|---|
| window | global |
| document | process |
| history(๋ธ๋ผ์ฐ์ ํ์คํ ๋ฆฌ) | module |
| location(๋ธ๋ผ์ฐ์ ํ์ฌ์์น, url ๋ฑ) | filename |
| navigator(ํ์ฌ ์ฌ์ฉ์ค์ธ ๋ธ๋ผ์ฐ์ ์ ๋ํ ์ ๋ณด ๋ฑ) | require() |
๋น์ทํ API๋ค(Node.js๊ฐ ๋ธ๋ผ์ฐ์ ์ Convention์ ๋ฐ๋ฅด๋ ๊ฒ)
| ๋ธ๋ผ์ฐ์ API | Node.js API |
|---|---|
| url | url |
| settimeout | settimeout |
๋ธ๋ผ์ฐ์ API์ Node.js API ๊ฐ์ ๊ฐ๊ฒฉ์ ์ค์ด๋ ค๊ณ ๋ ธ๋ ฅํจ