본문 바로가기

0/javascript

Node.js

반응형

An open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser.

JavaScript is used primarily for client-side scripting, in which scripts written in JavaScript are embedded in a webpage's HTML and run client-side by a JavaScript engine in the user's web browser.

Node.js lets developers use JavaScript to write command line tools and for server-side scripting--running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.

Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifying web application development around  a single programming language.

Node.js has an event-driven architecture capable of asynchronous I/O. These design choices aim to optimize throughput and scalability in web applications with many input/output operations, as well as for real-time Web applications.


Overview

Node.js allows the creation of Web servers and networking tools using JavaScript and a collection of "modules" that handle various core functionality. Modules are provided for file system I/O, networking(DNS, HTTP, TCP, TLS/SSL, or UDP), binary data (buffers), cryptography functions, data streams, and other core functions. Node.js's modules use an API designed to reduce the complexity of writing server applications.

Node.js is primarily used to build network programs such as Web servers.

The biggest difference between Node.js and PHP is that most functions in PHP block until completion (commands execute only after previous commands finish), while Node.js functions are non-blocking (commands execute concurrently or even in parallel, and use callbacks to signal completion or failure).


Platform architecture

Node.js brings event-driven programming to web servers, enabling development of fast web servers in JavaScript.

Developers can create scalable servers without using threading, by using a simplified model of even driven programming that uses callbacks to signal the completion of a task.

Node.js connects the ease of a script language (JavaScript) with the power of Unix network programming.

Node.js was built on the Google V8 JavaScript engine since it was open-sources under the BSD license. It is proficient with internet fundamentals such as HTTP, DNS, TCP. 

Also, JavaScript was a well-known language, making Node.js accessible to the web development community.


Technical details

Node.js is a JavaScript runtime environment that processes incoming requests in a loop, called the event loop.


Threading

Node.js operates on a single thread event loop, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections without incurring the cost of thread context switching.

The design of sharing a single thread among all the requests that use the observer pattern is intended for building highly concurrent applications, where any function performing I/O must use a callback. To accommodate the single-threaded event loop, Node.js uses the libuv library-which, in turn, uses a fixed-sized thread pool that handles some of the non-blocking asynchronous I/O operations.

A thread pool handles execution of parallel tasks in Node.js. The main thread call functions post tasks to the shared task queue, which threads in the thread pool pull and execute.

Inherently non-blocking system functions such as networking translate to kernel-side non-blocking sockets, while inherently blocking system functions such as file I/O run in a blocking way on their own threads. When a thread in the thread pool completes a task, it informs the main thread of this, which in turn, wakes up and executes the registered callback.

A downside of this single-threaded approach is that Node.js doesn't allow vertical scaling by increasing the number of CPU cores of the machine it is running on without using an additional module. However, developers can increase the default number of threads in the libuv thread pool. The server operating system is likely to distribute these threads across multiple cores. Another problem is that long lasting computations and other CPU-bound tasks freeze the entire event-loop until completion.


V8

V8 is the JavaScript execution engine which was initially built for Google Chrome. It was then open-sourced by Google in 2008. Written in C++, V8 compiles JavaScript source code to native machine code instead of interpreting it in real time.

Node.js uses libuv to handle asynchronous events. Libuv is an abstraction layer for network and file system functionality on both Windows and POSIX-based systems such as Linux, macOS, OSS on NonStop, and Unix.

The core functionality of Node.js resides in a JavaScript library. The Node.js bindings, written in C++, connect these technologies to each other and to the operating system.


Package management

npm is the pre-installed package manager for the Node.js server platform. It installs Node.js programs from the npm registry, organizing the installation and management of third-party Node.js programs. Packages in the npm registry can range from simple helper libraries such as Lodash to task runners such as Grunt.


Unified API

Node.js can be combined with a browser, a database that supports JSON data and JSON for a unified JavaScript development stack. With the adaptation of what were essentially server-side development patterns such as MVC, MVP, MVVM, etc., Node.js allows the reuse of the same model and service interface between client-side and server-side.


Event loop

Node.js registers with the operating system so the OS notifies it of connections and issues a callback. Within the Node.js runtime, each connection is a small heap allocation. Traditionally, relatively heavyweight OS processes or threads handled each connection. Node.js uses an event loop for scalability, instead of processes or threads. In contrast to other event-driven servers, Node.js's event loop does not need to be called explicitly. Instead callbacks are defined, and the server automatically enters the event loop at the end of the callback definition. Node.js exits the event loop when there are no further callbacks to be performed.



* 런타임이란?

- 컴퓨터 과학에서 컴퓨터 프로그램이 실행되고 있는 동안의 동작을 말합니다. "런타임"이라는 용어는 컴퓨터 언어 안에 쓰인 프로그램을 관리하기 위해 특정한 컴파일러나 가상머신이 사용하는 기본 코드의 라이브러리나 프로그램을 가리키는 런타임 라이브러리라고도 일컫습니다.

- 런타임 환경은 컴퓨터가 실행되는 동안 프로세스나 프로그램을 위한 소프트웨어 서비스를 제공하는 가상 머신의 상태입니다. 운영 체제 자체에 속하는 경우도 있고 운영 체제에서 작동하는 소프트웨어를 뜻할 수도 있습니다.

- 쉽게 말해서 프로그래밍 언어가 구동되는 환경이라고 이해하면 됩니다.

ex) javascript가 web browser에서 작동하는경우, browser가 런타임이고 node.js라는 환경에서 구동이 되면 node.js를 런타임이라고 보시면 됩니다.


따라서, node.js는 웹 서버가 아니라 JavaScript를 실행시켜주는 런타임일뿐입니다.


참고 :

https://en.wikipedia.org/wiki/Node.js

http://asfirstalways.tistory.com/99

https://velopert.com/133

반응형