{"id":161921,"date":"2021-01-24T17:00:50","date_gmt":"2021-01-24T14:00:50","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/here-are-15-of-the-most-asked-node-js-interview-questions\/"},"modified":"2021-01-24T17:00:50","modified_gmt":"2021-01-24T14:00:50","slug":"here-are-15-of-the-most-asked-node-js-interview-questions","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/","title":{"rendered":"#Here are 15 of the most asked Node.js interview questions"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2eaa97be4da\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2eaa97be4da\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#How_is_Nodejs_different_from_Javascript\" >How is Node.js different from Javascript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#When_should_you_use_Nodejs\" >When should you use Node.js?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_does_EventEmitter_do\" >What does EventEmitter do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_is_Nodes_event_loop\" >What is Node\u2019s event loop?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_are_Node_Streams\" >What are Node Streams?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_is_the_difference_between_readFile_and_createReadStream_functions\" >What is the difference between readFile and createReadStream functions?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#How_do_you_handle_uncaught_exceptions_in_Nodejs\" >How do you handle uncaught exceptions in Node.js?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#Can_Node_take_full_advantage_of_a_multi-processor_system\" >Can Node take full advantage of a multi-processor system?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_is_the_reactor_design_pattern_used_in_Nodejs\" >What is the reactor design pattern used in Node.js?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_are_the_benefits_of_a_single-threaded_web_backend_to_a_multi-threaded_one\" >What are the benefits of a single-threaded web backend to a multi-threaded one?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_is_REPL_in_Node\" >What is REPL in Node?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_are_stubs\" >What are stubs?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#Why_is_it_a_good_practice_to_separate_%E2%80%98app_and_%E2%80%98server_in_Express\" >Why is it a good practice to separate \u2018app\u2019 and \u2018server\u2019 in Express?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#What_are_yarn_and_npm_Why_would_you_want_to_use_yarn_over_npm\" >What are yarn and npm? Why would you want to use yarn over npm?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-15-of-the-most-asked-node-js-interview-questions\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#Here are 15 of the most asked Node.js interview questions<\/strong>&#8221;<\/p>\n<div>\n                            Got a programming interview coming up? Preparing for it is as important as developing your coding\u00a0knowledge to ace it.\u00a0It\u2019ll give you the confidence to handle the interview and shake off the jitters. This is especially true if you are facing a programming interview for the first time in your life.<\/p>\n<p>To help Node.js developers achieve the necessary preparedness for an interview, I have put together a list of 15 commonly asked Node.js- and web development-related interview questions. These questions and their answers will also prompt you to brush up on any areas you feel need improvement before the big interview.<\/p>\n<p>In this post, we are focusing on questions related to only Node.js; however, bear in mind that Java<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>-related questions are very common in\u00a0Node.js interviews, so you should prepare for some of those as well. We wrote a post not so long ago on<span>\u00a0<\/span><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/20-common-javascript-interview-questions\/\">common JavaScript interview questions<\/a>\u00a0to cover the basics.<\/p>\n<p>Right now, let\u2019s dive in to see the Node-related questions you are likely to field in your next interview.<\/p>\n<h2 id=\"how-is-nodejs-different-from-javascript\"><span class=\"ez-toc-section\" id=\"How_is_Nodejs_different_from_Javascript\"><\/span>How is Node.js different from Javascript?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335083 lazy\" alt=\"\" width=\"707\" height=\"595\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.22.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.22.png 1058w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.22-250x210.png 250w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.22-321x270.png 321w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.22-160x135.png 160w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.22-796x670.png 796w\"\/><\/figure>\n<h2 id=\"when-should-and-shouldnt-you-use-nodejs\"><span class=\"ez-toc-section\" id=\"When_should_you_use_Nodejs\"><\/span>When should you use Node.js?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node.js is asynchronous, event-driven, non-blocking, and single-threaded. It makes Node a perfect candidate for developing the following types of applications:<\/p>\n<ul>\n<li>Realtime applications like chat and services delivering\u00a0live updates.<\/li>\n<li>Streaming applications that deliver\u00a0video or other multi<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a> content to a large audience.<\/li>\n<li>I\/O intensive applications, like collaborative platforms.<\/li>\n<li>Web backends that follow microservices architecture.<\/li>\n<\/ul>\n<p>However, Node.js\u2019 unique qualities make it less than ideal\u00a0for some other types of applications: those\u00a0that carry out CPU-intensive tasks like complex mathematical computations will be restricted by\u00a0Node\u2019s single-threaded\u00a0execution.<\/p>\n<p>If you want to learn more about this, check out our article on\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/when-you-should-and-should-not-use-nodejs-for-your-project\/\">Node.js architecture and when to use Node.js in projects<\/a>.<\/p>\n<h2 id=\"what-does-eventemitter-do\"><span class=\"ez-toc-section\" id=\"What_does_EventEmitter_do\"><\/span>What does EventEmitter do?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Every object in Node.js capable of emitting events is a member of the<span>\u00a0<\/span><code>EventEmitter<\/code><span>\u00a0<\/span>class. The<span>\u00a0<\/span><code>http<\/code>\u00a0module is one such example.<\/p>\n<p>All<span>\u00a0<\/span><code>EventEmitter<\/code><span>\u00a0<\/span>classes can use the<span>\u00a0<\/span><code>eventEmitter.on()<\/code><span>\u00a0function<\/span> to attach event listeners to the event. Then, as soon as such an event is caught, its listeners are called one by one synchronously.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335084 lazy\" alt=\"\" width=\"827\" height=\"480\" sizes=\"auto, (max-width: 827px) 100vw, 827px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.28.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.28.png 1034w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.28-280x162.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.28-465x270.png 465w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.28-233x135.png 233w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.31.28-796x462.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<h2 id=\"what-is-nodes-event-loop\"><span class=\"ez-toc-section\" id=\"What_is_Nodes_event_loop\"><\/span>What is Node\u2019s event loop?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Since Node.js is single-threaded, it has to be non-blocking to prevent the thread from spending too long on a task that takes a while\u00a0to complete. The event loop is responsible for enabling\u00a0this non-blocking behavior.\u00a0Its job is <span style=\"background-color: rgba(46, 146, 255, 0.2);\">to schedule<\/span> pending tasks using the application thread.<\/p>\n<p>We know that Node uses callbacks to handle the response returned by an asynchronous function when its task is complete. Similar to the event that created the task, the completion of the task also emits an event. Node.js adds these events that require handling to an event queue.<\/p>\n<p>The event loop iterates over events in the event queue and schedules when to execute their associated callback functions.<\/p>\n<h2 id=\"what-are-node-streams\"><span class=\"ez-toc-section\" id=\"What_are_Node_Streams\"><\/span>What are Node Streams?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Streams are pipelines that read or write data from a source and transfer it to a continuous flow destination. There are four types of streams:<\/p>\n<ul>\n<li>Readable<\/li>\n<li>Writable<\/li>\n<li>Duplex (both readable and writable)<\/li>\n<li>Transform (A type of duplex stream. Its output is calculated using the input)<\/li>\n<\/ul>\n<p>Each stream is also an<span>\u00a0<\/span><code>EventEmitter<\/code>. It means a stream object can emit events when there is no data on the stream, when data is available on the stream, or when data in the stream is flushed from the program.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335085 lazy\" alt=\"\" width=\"776\" height=\"478\" sizes=\"auto, (max-width: 776px) 100vw, 776px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.11.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.11.png 1046w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.11-280x172.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.11-439x270.png 439w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.11-219x135.png 219w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.11-796x490.png 796w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.11-1044x644.png 1044w\"\/><\/figure>\n<p><\/span><\/p>\n<h2 id=\"what-is-the-difference-between-readfile-and-createreadstream-functions\"><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_readFile_and_createReadStream_functions\"><\/span>What is the difference between readFile and createReadStream functions?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>readFile<\/code><span>\u00a0function<\/span> reads the entire content of the file asynchronously and stores it in the memory before passing it to the user.<\/p>\n<p><code>createReadStream<\/code><span>\u00a0<\/span>uses a readable stream that would read the file chunk by chunk without storing the entirety of it into the memory.<\/p>\n<p><code>createReadStream<\/code><span>\u00a0<\/span>optimizes the file reading operation compared to<span>\u00a0<\/span><code>readFile<\/code><span>\u00a0<\/span>by using less memory and making it faster. If the file is of considerable size, the user doesn\u2019t have to wait a long time until its entire content is available, because small chunks are sent to the user as they are read.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335086 lazy\" alt=\"\" width=\"746\" height=\"241\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.46.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.46.png 1042w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.46-280x90.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.46-540x174.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.46-270x87.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.46-796x257.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<h2 id=\"how-do-you-handle-uncaught-exceptions-in-nodejs\"><span class=\"ez-toc-section\" id=\"How_do_you_handle_uncaught_exceptions_in_Nodejs\"><\/span>How do you handle uncaught exceptions in Node.js?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can catch uncaught exceptions thrown in the application at its process level. We attach a listener to the<span>\u00a0<\/span><code>process<\/code><span>\u00a0<\/span>global object to catch such events.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335087 lazy\" alt=\"\" width=\"781\" height=\"176\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.50.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.50.png 1054w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.50-280x63.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.50-540x122.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.50-270x61.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.33.50-796x180.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<h2 id=\"can-node-take-full-advantage-of-a-multi-processor-system\"><span class=\"ez-toc-section\" id=\"Can_Node_take_full_advantage_of_a_multi-processor_system\"><\/span>Can Node take full advantage of a multi-processor system?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node applications are always single-threaded. So, naturally, the application uses only a single processor even when running on multi-processor systems.<\/p>\n<p>But one of Node\u2019s core modules, Cluster, provides support for Node applications to take advantage of multiple cores. It allows us to create multiple worker processes that can run on several cores in parallel, and share a single port to listen to events.<\/p>\n<p>Here, each process uses IPC to communicate with the main thread and pass the server handle to others as needed. The main process can either listen to the port itself and pass every new connection to child processes in a round robin order, or assign the port to child processes so the child processes listen to requests.<\/p>\n<h2 id=\"what-is-the-reactor-design-pattern-used-in-nodejs\"><span class=\"ez-toc-section\" id=\"What_is_the_reactor_design_pattern_used_in_Nodejs\"><\/span>What is the reactor design pattern used in Node.js?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The reactor pattern is used for maintaining non-blocking I\/O operations in Node.js. It attaches a callback function (a handler) to each I\/O operation. The handler is then submitted to a demultiplexer at the time of request creation.<\/p>\n<p>Demultiplexer collects every I\/O request made in the application and queues them as events in a queue. This is what we call the event queue. After queuing the event, the demultiplexer returns the control of the application thread.<\/p>\n<p>Meanwhile, the event loop iterates over each event in the event queue, and invokes the attached callback to handle the event response.<\/p>\n<p>This is the reactor pattern used by Node.js.<\/p>\n<h2 id=\"what-are-the-benefits-of-a-single-threaded-web-backend-to-a-multi-threaded-one\"><span class=\"ez-toc-section\" id=\"What_are_the_benefits_of_a_single-threaded_web_backend_to_a_multi-threaded_one\"><\/span>What are the benefits of a single-threaded web backend to a multi-threaded one?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Put another way: though Node is single-threaded, most of the programming languages used for backend development provides multiple threads to handle application operations. In what way is having only a single thread is beneficial to backend development?<\/p>\n<ul>\n<li>It\u2019s easier for developers to implement\u00a0applications. Our applications have no risk of running into unexpected race conditions suddenly while in production.<\/li>\n<li>Single-threaded applications are easily scalable.<\/li>\n<li>They can serve a large number of user requests received at a moment without much delay. In comparison, a multi-threaded backend has to wait for a thread from the thread pool to be free to serve the user request when\u00a0traffic is high. With Node\u2019s non-blocking nature, there\u2019s no risk of a user request hanging on to the single thread for too long (this is true only when the operations are not CPU-intensive).<\/li>\n<\/ul>\n<h2 id=\"what-is-repl-in-node\"><span class=\"ez-toc-section\" id=\"What_is_REPL_in_Node\"><\/span>What is REPL in Node?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>REPL stands for Read-Eval-Print-Loop. It is a virtual environment where you can run a programming language easily. Node comes with a built-in REPL to run JavaScript code. It is similar to the consoles we use in browsers to run JavaScript code.<\/p>\n<p>To start the Node REPL, you just have to run the command, node, on the command-line. Then, once you write a line of\u00a0JavaScript\u00a0code, you can subsequently see its output.<\/p>\n<p>The callback passed to the setImmediate function is executed in the next iteration of the event loop over the event queue.<\/p>\n<p>On the other hand, the callback passed to the<span>\u00a0<\/span><code>process.nextTick<\/code><span>\u00a0<\/span>is executed before the next iteration of the event loop, and after the operation currently running in the program is finished. At the application start, its callback is called before the event loop starts iterating over the event queue.<\/p>\n<p>Therefore, the <code>process.nextTick<\/code><span>\u00a0callback\u00a0<\/span>is always called before the <code>setImmediate<\/code>\u00a0callback.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335088 lazy\" alt=\"\" width=\"797\" height=\"668\" sizes=\"auto, (max-width: 797px) 100vw, 797px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.34.36.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.34.36.png 1058w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.34.36-251x210.png 251w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.34.36-322x270.png 322w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.34.36-161x135.png 161w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.34.36-796x667.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<h2 id=\"what-are-stubs\"><span class=\"ez-toc-section\" id=\"What_are_stubs\"><\/span>What are stubs?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Stubs are used when testing applications. They simulate the behavior of a given component or a module so that you can focus on just the part of the code you want to test. By using stubs in place of components irrelevant to the test, you won\u2019t have to worry about external components impacting the results.<\/p>\n<p>For example, if the component you are testing has a file reading operation before the part you expect to test, you can use a stub to simulate that behavior and return mock content without actually reading the file.<\/p>\n<p>In Node, we use libraries like Sinon for this purpose.<\/p>\n<h2 id=\"why-is-it-a-good-practice-to-separate-app-and-server-in-express\"><span class=\"ez-toc-section\" id=\"Why_is_it_a_good_practice_to_separate_%E2%80%98app_and_%E2%80%98server_in_Express\"><\/span>Why is it a good practice to separate \u2018app\u2019 and \u2018server\u2019 in Express?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>By separating app and server in Express, we can separate the API implementation from the network-related configuration. This allows us to carry out API tests without performing network calls. This also guarantees faster test execution and better code coverage metrics.<\/p>\n<p>To achieve this separation, you should declare API and server in separate files. Here we use two files:<span>\u00a0<\/span><code>app.js<\/code><span>\u00a0<\/span>and<span>\u00a0<\/span><code>server.js<\/code>.<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1335089 lazy\" alt=\"\" width=\"784\" height=\"742\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.35.09.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.35.09.png 1060w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.35.09-222x210.png 222w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.35.09-285x270.png 285w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.35.09-143x135.png 143w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-22-at-09.35.09-796x754.png 796w\"\/><\/figure>\n<p><\/span><\/p>\n<h2 id=\"what-are-yarn-and-npm-why-would-you-want-to-use-yarn-over-npm\"><span class=\"ez-toc-section\" id=\"What_are_yarn_and_npm_Why_would_you_want_to_use_yarn_over_npm\"><\/span>What are yarn and npm? Why would you want to use yarn over npm?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>npm<\/code><span>\u00a0<\/span>is the default package manager distributed with Node.js. It has a large library of public and private packages stored in a database called \u2019emp registry\u2019 that users can access via npm\u2019s command-line client. With the help of npm, users can easily manage dependencies used in a project.<\/p>\n<p><code>yarn<\/code><span>\u00a0<\/span>is also a package manager that was released as an answer to some of npm\u2019s shortcomings. However, yarn relies on the npm registry to provide users access to packages. Since yarn\u2019s underlying structure is based on npm itself, your project structure and workflow doesn\u2019t have to go through major changes if you are migrating to yarn from npm.<\/p>\n<p>Like I mentioned before, yarn provides better functionality over npm in some cases. Unlike npm, it caches every package you download, so you don\u2019t have to redownload it whenever needed.<\/p>\n<p>It also provides better security by verifying the integrity of packages using checksums. It guarantees a package that worked on a certain system will work exactly the same way in any other system.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this post, we went through 15 of the most commonly asked Node.js interview questions to help you prepare better for your next interview. Knowing what type of questions you are likely to get asked and knowing their answers will give you the confidence to answer interview questions without feeling nervous.<\/p>\n<p><i><span style=\"font-weight: 400;\">This <\/span><\/i><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/15-commonly-asked-nodejs-interview-questions\/\"><i><span style=\"font-weight: 400;\">article<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> was originally published on <\/span><\/i><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/\"><i><span style=\"font-weight: 400;\">Live Code Stream<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> by <\/span><\/i><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\"><i><span style=\"font-weight: 400;\">Juan Cruz Martinez<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> (Twitter: <\/span><\/i><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/twitter.com\/bajcmartinez\"><i><span style=\"font-weight: 400;\">@bajcmartinez<\/span><\/i><\/a><i><span style=\"font-weight: 400;\">), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/subscribe\"><i><span style=\"font-weight: 400;\">Live Code Stream<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> is also available as a free weekly <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/news\/\" data-internallinksmanager029f6b8e52c=\"2\" title=\"News\" target=\"_blank\" rel=\"noopener\">news<\/a>letter. Sign up for updates on everything related to programming, AI, and computer <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/sciencee\/\" data-internallinksmanager029f6b8e52c=\"5\" title=\"Science\" target=\"_blank\" rel=\"noopener\">science<\/a> in <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a>.<\/span><\/i><\/p>\n<\/p><\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/24\/here-are-15-of-the-most-asked-node-js-interview-questions\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Here are 15 of the most asked Node.js interview questions&#8221; Got a programming interview coming up? Preparing for it is as important as developing your coding\u00a0knowledge to ace it.\u00a0It\u2019ll give you the confidence to handle the interview and shake off the jitters. This is especially true if you are facing a programming interview for the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":161922,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/1-copy-47.jpg&signature=bceed88e25e9ab44cbd194e3af53aacf","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[75083,88987,70759],"class_list":["post-161921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-developer","tag-node-js","tag-tech"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/161921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=161921"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/161921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/161922"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=161921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=161921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=161921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}