{"id":8190,"date":"2024-09-03T17:43:09","date_gmt":"2024-09-03T12:13:09","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8190"},"modified":"2024-09-03T17:43:14","modified_gmt":"2024-09-03T12:13:14","slug":"node-js-2","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/node-js-2\/","title":{"rendered":"Node JS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>What is Node JS and how it is built?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Node JS is a javascript runtime environment built on a Chrome V8 JS engine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Earlier we could run JS only in the browser with the help of the JS Engine which is present inside the browser<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Chrome -&gt; V8 (Most Popular) (Written in C++)<\/li>\n\n\n\n<li>Firefox -&gt; Spider Monkey<\/li>\n\n\n\n<li>Safari -&gt; JavaScriptCore (also known as Nitro)<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Ryan Dhal combined the V8 engine with the C++ which resulted in the formation of Node JS<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we can run the JS outside of the browser<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">JS can talk to native machines because of C++<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can also create web servers in the JS language<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a cross-platform, open-source maintained by Open JS Foundation<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Inside working of Node JS<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeD9oCj9XY6VtEtrKdUDAH_7d4gL_-Ydyx9hUwWgpnBU8BrnMGXjhh0a9ADgBP9QUrifH08qbvmZE49Sh3mluzteV5C87196B9xQioSOdIsMWVwIcFzFWGj_uGxsbutlVv9fBi4t8G1Dgme0AyYeiDWOqs?key=iWfketVkh4jZEOREu8lOAQ\" alt=\"\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Clients and Requests:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Client-1 and Client-2 represent users or applications sending requests to the Node.js server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Request-1 and Request-2 are the tasks or operations these clients want the server to perform (e.g., reading a file, or accessing a database).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Event Queue:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a request is received by the server, it is placed in the Event Queue.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Event Queue is like a waiting line where all incoming requests are stored until they can be processed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. Single Thread Event Loop:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Event Loop is the core part of Node.js. It continuously checks the Event Queue to see if there are any tasks (requests) that need to be processed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Node.js operates on a single thread, which means there is only one main path of execution that handles all tasks. However, it is highly efficient because it doesn&#8217;t wait for tasks to complete before moving on to the next one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. Non-Blocking I\/O Requests:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Event Loop checks if a request is non-blocking (does not require waiting, like reading from memory).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the request is non-blocking, it is processed immediately within the Event Loop, and the response is sent back to the client. This allows Node.js to handle many requests very quickly without waiting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5. Blocking I\/O Requests:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the request is blocking (requires waiting, like accessing a database or reading a file from disk), the Event Loop cannot process it immediately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead, it sends the blocking request to a Thread Pool (handled by libuv, a library used in Node.js).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6. libuv Thread Pool:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Thread Pool contains multiple threads (e.g., T1, T2, T3, etc.) that can handle blocking tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the Thread Pool finishes processing a blocking request, it sends the result back to the Event Loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>7. Sending Responses:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the Event Loop receives the response from the Thread Pool (for blocking requests) or processes it directly (for non-blocking requests), it sends the response back to the appropriate client.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>8. Efficiency:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By using this approach, Node.js can efficiently handle many client requests at once without being slowed down by long-running operations. Only the tasks that need waiting (blocking I\/O) are sent to the Thread Pool, while other tasks are handled immediately, keeping the system responsive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Node JS and how it is built? Node JS is a javascript runtime environment built on a Chrome [&hellip;]<\/p>\n","protected":false},"author":507,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-8190","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-286","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/507"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8190"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8190\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}