{"id":165,"date":"2019-12-06T11:43:34","date_gmt":"2019-12-06T11:43:34","guid":{"rendered":"http:\/\/www.derniercriweb.solutions\/bury-blog\/?p=165"},"modified":"2026-04-08T12:12:03","modified_gmt":"2026-04-08T11:12:03","slug":"node","status":"publish","type":"post","link":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/2019\/12\/06\/node\/","title":{"rendered":"Node"},"content":{"rendered":"\n<h3 class=\"wp-block-heading has-accent-color has-text-color\">Intro<\/h3>\n\n\n\n<p class=\"has-drop-cap\"><strong>Node.js<\/strong>&nbsp;is an&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Open-source_software\">open-source<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cross-platform\">cross-platform<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/JavaScript\">JavaScript<\/a>&nbsp;runtime environment that executes JavaScript code outside of a browser. Node.js lets developers use JavaScript to write command line tools and for&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Server-side_scripting\">server-side scripting<\/a>\u2014running scripts server-side to produce&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Dynamic_web_page\">dynamic web page<\/a>&nbsp;content before the page is sent to the user&#8217;s web browser. Consequently, Node.js represents a &#8220;JavaScript everywhere&#8221; paradigm,&nbsp;unifying&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_application\">web-application<\/a>&nbsp;development around a single programming language, rather than different languages for server- and client-side scripts.<\/p>\n\n\n\n<p>Though&nbsp;<code>.js<\/code>&nbsp;is the standard&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Filename_extension\">filename extension<\/a>&nbsp;for JavaScript code, the name &#8220;Node.js&#8221; does not refer to a particular file in this context and is merely the name of the product. Node.js has an&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Event-driven_architecture\">event-driven architecture<\/a>&nbsp;capable of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Asynchronous_I\/O\">asynchronous I\/O<\/a>. These design choices aim to optimize&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Throughput\">throughput<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Scalability\">scalability<\/a>&nbsp;in web applications with many input\/output operations, as well as for&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Real-time_Web\">real-time Web<\/a>&nbsp;applications (e.g.,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Real-time_communication\">real-time communication<\/a>&nbsp;programs and&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Browser_game\">browser games<\/a>).<\/p>\n\n\n\n<p>The Node.js&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Distributed_development\">distributed development<\/a>&nbsp;project, governed by the Node.js Foundation,&nbsp;is facilitated by the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Linux_Foundation\">Linux Foundation<\/a>&#8216;s Collaborative Projects program<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-accent-color has-text-color\">Overview<\/h3>\n\n\n\n<p>Node.js allows the creation of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_server\">Web servers<\/a>&nbsp;and networking tools using&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/JavaScript\">JavaScript<\/a>&nbsp;and a collection of &#8220;modules&#8221; that handle various core functionalities.&nbsp;Modules are provided for&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/File_system\">file system<\/a>&nbsp;I\/O, networking (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Domain_Name_System\">DNS<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/HTTP\">HTTP<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Transmission_Control_Protocol\">TCP<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Transport_Layer_Security\">TLS\/SSL<\/a>, or&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/User_Datagram_Protocol\">UDP<\/a>),&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Binary_file\">binary<\/a>&nbsp;data (buffers),&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cryptography\">cryptography<\/a>&nbsp;functions,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Stream_(computing)\">data streams<\/a>, and other core functions.Node.js&#8217;s modules use an API designed to reduce the complexity of writing server applications.<\/p>\n\n\n\n<p>JavaScript is the only language that Node.js supports natively, but many&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Source-to-source_compiler\">compile-to-JS<\/a>&nbsp;languages are available. As a result, Node.js applications can be written in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/CoffeeScript\">CoffeeScript<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Dart_(programming_language)\">Dart<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/TypeScript\">TypeScript<\/a>,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/ClojureScript\">ClojureScript<\/a>&nbsp;and others.<\/p>\n\n\n\n<p>Node.js is primarily used to build network programs such as Web servers.The most significant difference between Node.js and&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/PHP\">PHP<\/a>&nbsp;is that most functions in PHP&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Asynchronous_I\/O\">block<\/a>&nbsp;until completion (commands only execute after previous commands finish), while Node.js functions are&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Asynchronous_I\/O\">non-blocking<\/a>&nbsp;(commands execute&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Concurrent_computing\">concurrently<\/a>&nbsp;or even in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Parallel_computing\">parallel<\/a>,&nbsp;and use&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Callback_(computer_programming)\">callbacks<\/a>&nbsp;to signal completion or failure).<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-accent-color has-text-color\">Technical Details<\/h3>\n\n\n\n<p>Node.js is a JavaScript runtime environment that processes incoming requests in a loop, called the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Event_loop\">event loop<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-accent-color has-text-color\">Threading<\/h4>\n\n\n\n<p>Node.js operates on a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Single_threading\">single-thread<\/a>&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Event_loop\">event loop<\/a>, using&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Non-blocking_I\/O\">non-blocking I\/O<\/a>&nbsp;calls, allowing it to support tens of thousands of concurrent connections without incurring the cost of thread&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Context_switch\">context switching<\/a>.&nbsp;The design of sharing a single thread among all the requests that use the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Observer_pattern\">observer pattern<\/a>&nbsp;is intended for building highly concurrent applications, where any function performing I\/O must use a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Callback_(computer_programming)\">callback<\/a>. To accommodate the single-threaded event loop, Node.js uses the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Libuv\">libuv<\/a>&nbsp;library\u2014which, in turn, uses a fixed-sized thread pool that handles some of the non-blocking asynchronous I\/O operations.<\/p>\n\n\n\n<p>A thread pool handles the execution of parallel tasks in Node.js. The main thread function call posts 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.<\/p>\n\n\n\n<p>A downside of this single-threaded approach is that Node.js doesn&#8217;t allow&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Vertical_scaling\">vertical scaling<\/a>&nbsp;by increasing the number of&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/CPU_core\">CPU cores<\/a>&nbsp;of the machine it is running on without using an additional module, such as cluster, StrongLoop Process Manager,&nbsp;or pm2. However, developers can increase the default number of threads in the libuv thread pool. The server&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Operating_system\">operating system (OS)<\/a>&nbsp;is likely to distribute these threads across multiple cores.&nbsp;Another problem is that long-lasting computations and other CPU-bound tasks freeze the entire event-loop until completion.<\/p>\n\n\n\n<p>Node.js uses&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Libuv\">libuv<\/a>&nbsp;to handle asynchronous events. Libuv is an abstraction layer for network and file system functionality on both Windows and&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/POSIX\">POSIX<\/a>-based systems such as Linux, macOS,&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Open-source_software\">OSS<\/a>&nbsp;on NonStop, and Unix.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-accent-color has-text-color\">V8<\/h4>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Chrome_V8\">V8<\/a>&nbsp;is the JavaScript execution engine which was initially built for&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Google_Chrome\">Google Chrome<\/a>. It was then open-sourced by Google in 2008. Written in&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B\">C++<\/a>, V8 compiles JavaScript source code to native&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Machine_code\">machine code<\/a>&nbsp;during runtime instead of interpreting it in ahead of time (AOT).<\/p>\n\n\n\n<h4 class=\"wp-block-heading has-accent-color has-text-color\">Package management<\/h4>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Npm_(software)\">npm<\/a>&nbsp;is the pre-installed package manager for the Node.js server platform. It installs Node.js programs from the&nbsp;<a href=\"https:\/\/www.npmjs.com\/\">npm registry<\/a>, organizing the installation and management of third-party Node.js programs. Packages in the npm registry can range from simple helper libraries such as&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Lodash\">Lodash<\/a>&nbsp;to task runners such as&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Grunt_(software)\">Grunt<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><a href=\"https:\/\/www.derniercriweb.co.uk\" target=\"_blank\" rel=\"noopener\">https:\/\/www.derniercriweb.co.uk<\/a><br><a href=\"https:\/\/derniercri.biz\/\" target=\"_blank\" rel=\"noopener\">https:\/\/derniercri.biz<\/a><br><a href=\"https:\/\/www.abportfolio.co.uk\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.abportfolio.co.uk<\/a><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Intro Node.js&nbsp;is an&nbsp;open-source,&nbsp;cross-platform,&nbsp;JavaScript&nbsp;runtime environment that executes JavaScript code outside of a browser. Node.js lets developers use JavaScript to write command line tools and for&nbsp;server-side scripting\u2014running scripts server-side to produce&nbsp;dynamic web page&nbsp;content before the page is sent to the user&#8217;s web browser. Consequently, Node.js represents a &#8220;JavaScript everywhere&#8221; paradigm,&nbsp;unifying&nbsp;web-application&nbsp;development around a single programming language, rather than [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":187,"comment_status":"closed","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[21,23,25,20,19],"class_list":["post-165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computing","tag-bury","tag-greater-manchester","tag-radcliffe","tag-web-design","tag-web-development"],"_links":{"self":[{"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":0,"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/media\/187"}],"wp:attachment":[{"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.derniercriweb.co.uk\/bury-blog\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}