<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Fabrizio Lallo</title>
    <description>The latest articles on Forem by Fabrizio Lallo (@fabriziolallo).</description>
    <link>https://forem.com/fabriziolallo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F938959%2F1ef700bd-aac0-4ca8-9721-58ab0cc7ffe7.jpeg</url>
      <title>Forem: Fabrizio Lallo</title>
      <link>https://forem.com/fabriziolallo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fabriziolallo"/>
    <language>en</language>
    <item>
      <title>4 FREE Resources To Boost Your Node.js Developer Career ⚡</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Sun, 04 Dec 2022 23:00:00 +0000</pubDate>
      <link>https://forem.com/nodedoctors/4-free-resources-to-boost-your-nodejs-developer-career-1bpe</link>
      <guid>https://forem.com/nodedoctors/4-free-resources-to-boost-your-nodejs-developer-career-1bpe</guid>
      <description>&lt;p&gt;Do you want to &lt;strong&gt;speed up&lt;/strong&gt; your &lt;strong&gt;Node.js&lt;/strong&gt; career?&lt;/p&gt;

&lt;p&gt;These are the resources I wish I knew when I started learning Node.js, and guess what? They are completely &lt;strong&gt;FREE&lt;/strong&gt;:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Event Loop 🔁
&lt;/h2&gt;

&lt;p&gt;An animated guide to understanding how Event Loop works under the hood.&lt;/p&gt;

&lt;p&gt;What's better than learning through animations and graphics?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✍🏻 &lt;em&gt;Andrew Hu&lt;/em&gt; - &lt;a href="https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62"&gt;https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;✍🏻 &lt;em&gt;Fabrizio Lallo&lt;/em&gt; - &lt;a href="https://dev.to/nodedoctors/animated-nodejs-event-loop-phases-1mcp"&gt;https://dev.to/nodedoctors/animated-nodejs-event-loop-phases-1mcp&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Broken Promises 💔
&lt;/h2&gt;

&lt;p&gt;Using promises correctly is crucial to maintain Node.js performance and writing clean code.&lt;/p&gt;

&lt;p&gt;✍🏻 &lt;em&gt;James Snell &amp;amp; Matteo Collina&lt;/em&gt;- &lt;a href="https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Async Iterators 🔃
&lt;/h2&gt;

&lt;p&gt;Knowing iteration protocols is essential to write more modern and idiomatic Javascript code.&lt;/p&gt;

&lt;p&gt;✍🏻 &lt;em&gt;Luciano Mammino&lt;/em&gt; - &lt;a href="https://github.com/lmammino/iteration-protocols-workshop" rel="noopener noreferrer"&gt;https://github.com/lmammino/iteration-protocols-workshop&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Streams 🚰
&lt;/h2&gt;

&lt;p&gt;Learning streams allow us to build efficient Node.js applications.&lt;/p&gt;

&lt;p&gt;You can build a video streaming service or ingest and transform terabytes of data, which is in great demand nowadays.&lt;/p&gt;

&lt;p&gt;✍🏻 &lt;em&gt;Luciano Mammino&lt;/em&gt; - &lt;a href="https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=yRyfr1Qcf34&amp;amp;t=1181s&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;&lt;strong&gt;Let's Connect&lt;/strong&gt;&lt;/em&gt; 🌎 &lt;br&gt;
&lt;em&gt;Follow us on Twitter &lt;a href="https://twitter.com/FabriLallo" rel="noopener noreferrer"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368" rel="noopener noreferrer"&gt;@AndrewHu368&lt;/a&gt; for more Node.js content&lt;/em&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>oop</category>
    </item>
    <item>
      <title>5 Top Node.js Streams Resources You Should Learn to Level Up Your Node.js Skills 🚀💯</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Fri, 02 Dec 2022 12:27:31 +0000</pubDate>
      <link>https://forem.com/nodedoctors/5-top-nodejs-streams-resources-you-should-learn-to-level-up-your-nodejs-skills-3d88</link>
      <guid>https://forem.com/nodedoctors/5-top-nodejs-streams-resources-you-should-learn-to-level-up-your-nodejs-skills-3d88</guid>
      <description>&lt;p&gt;If you want to master Node.js, stop screwing around 🛑.&lt;/p&gt;

&lt;p&gt;Learn &lt;strong&gt;Streams.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Streams are the fundamental block of the essential Node.js features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTTP server&lt;/li&gt;
&lt;li&gt;File management&lt;/li&gt;
&lt;li&gt;Encryption and Decryption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means you can build (TODAY) a video streaming service or ingest and transform terabytes of data just by learning streams.&lt;/p&gt;

&lt;p&gt;You can use these resources and unlock all the Node.js power 🔐🔥.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Streams workshop 📒
&lt;/h2&gt;

&lt;p&gt;Don’t you know anything about theory and practice about streams? This workshop is the gold standard and the perfect place to get started with streams—an ideal combination of theory and practical exercises.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Luciano Mammino&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://github.com/lmammino/streams-workshop"&gt;https://github.com/lmammino/streams-workshop&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;strong&gt;Everything you should know about Node.js Streams 📽️&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you are more of a “video guy,” these are fantastic walkthrough about how streams work and how you can use them in your project.&lt;/p&gt;

&lt;p&gt;PLUS 🆕: the first video contains the new stream features introduced in Node.js v18.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Erik Wendell&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://youtu.be/BdePYKgrMh0"&gt;https://youtu.be/BdePYKgrMh0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;EXTRA&lt;/em&gt;&lt;/strong&gt;:&lt;br&gt;
Author ✍🏻: Luciano Mammino&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://youtu.be/ldcfYB_mo6Q"&gt;https://youtu.be/ldcfYB_mo6Q&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;strong&gt;Understanding Streams in Node.js 🔁&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Modern Node.js streams with async iterator.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Liz Parody&lt;/p&gt;

&lt;p&gt;Link 🔗:  &lt;a href="https://nodesource.com/blog/understanding-streams-in-nodejs/"&gt;https://nodesource.com/blog/understanding-streams-in-nodejs/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;strong&gt;NodeJS Backpressuring in Streams 🚱&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Backpressure is an important technique to prevent your application from being overwhelmed by data. It is essential to understand and master backpressure.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Vladimir Topolev&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://enlear.academy/nodejs-backpressuring-in-streams-52638f505e1b"&gt;https://enlear.academy/nodejs-backpressuring-in-streams-52638f505e1b&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. &lt;strong&gt;How To Code A Video Streaming Server in NodeJS 🕹️&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;All Node.js stream knowledge is put into practice to build a real-world example: a video streaming service.&lt;/p&gt;

&lt;p&gt;Author ✍🏻: Abdisalan Mohamud&lt;/p&gt;

&lt;p&gt;Link 🔗: &lt;a href="https://youtu.be/ZjBLbXUuyWg"&gt;https://youtu.be/ZjBLbXUuyWg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, follow us on Twitter &lt;a href="https://twitter.com/FabriLallo"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368"&gt;@AndrewHu368&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Credits to Conny Schneider&lt;/em&gt;&lt;br&gt;
&lt;a href="https://unsplash.com/photos/pREq0ns_p_E"&gt;https://unsplash.com/photos/pREq0ns_p_E&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Node.js animated: Event Loop Phases</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Sat, 19 Nov 2022 20:52:26 +0000</pubDate>
      <link>https://forem.com/nodedoctors/animated-nodejs-event-loop-phases-1mcp</link>
      <guid>https://forem.com/nodedoctors/animated-nodejs-event-loop-phases-1mcp</guid>
      <description>&lt;p&gt;In the previous article, &lt;a href="https://dev.to/nodedoctors/an-animated-guide-to-nodejs-event-loop-3g62"&gt;Node.js animated: Event Loop&lt;/a&gt;, you explored how Libuv and the event loop enable asynchronous task handling in Node.js' single-threaded environment. We simplified the event loop as a mechanism that pushes callbacks from a &lt;strong&gt;single event queue&lt;/strong&gt; to an empty call stack. In reality, the event loop is composed of multiple phases, each responsible for specific asynchronous tasks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkc1k3qhlvj0hrts6ud3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkc1k3qhlvj0hrts6ud3.gif" alt="event_loop_phases" width="1080" height="608"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The event loop phases
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A phase is a FIFO queue of callbacks to execute. When the event loop enters a given phase, it executes callbacks until the queue is exhausted or the maximum number of callbacks is run and moves to the next stage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The event loop has &lt;strong&gt;6&lt;/strong&gt; phases, and they run in the following order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Timers&lt;/strong&gt;: executes callbacks scheduled by setTimeout() and setInterval().&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pending callbacks&lt;/strong&gt;: executes I/O callbacks deferred to the next loop iteration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idle-prepare&lt;/strong&gt;: only used internally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poll&lt;/strong&gt;: retrieve new I/O events and execute I/O-related callbacks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check&lt;/strong&gt;: executes callbacks scheduled by setImmediate().&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Close callbacks&lt;/strong&gt;: some close callbacks, e.g., socket.on('close', …).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F332ug2w37qhov5fgvbv4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F332ug2w37qhov5fgvbv4.gif" alt="event_loop_phases_handled" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The event loop doesn’t constantly keep spinning. If there are no active I/O handlers and the event loop doesn’t need to process any callbacks, the Node.js program automatically exits. On the other hand, the event loop stops on the poll phase to capture incoming requests whenever you create a web server and the phases are empty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Timers and check phase
&lt;/h2&gt;

&lt;p&gt;You are ready to apply your newly acquired knowledge. Still, you can be disappointed when running a &lt;code&gt;setTimeout&lt;/code&gt; after a &lt;code&gt;setImmediate&lt;/code&gt;, or vice versa, because the result is not deterministic.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setTimeout&lt;/code&gt; schedules a callback after &lt;strong&gt;at least&lt;/strong&gt; the defined milliseconds have elapsed. The CPU could be busy and Libuv is unable to set the task as complete before the event loop processes the timer phase. Consequently, the setImmediate callback could be run in advance, although the timer is the first phase.&lt;/p&gt;

&lt;p&gt;In this example, the CPU is not busy, and the &lt;code&gt;setTimeout&lt;/code&gt; callback is immediately added to its dedicated queue before &lt;code&gt;setImmedate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/772833707" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In this second example, the event loop starts processing the timer phase, but the timer has not expired yet, so setImmediate callback is run in advance.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/772834525" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring the poll phase
&lt;/h2&gt;

&lt;p&gt;You are now wondering how I can test the phases processing order. The following code snippet will help you prove the order of the phases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjla2set11e20tt5k9v9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjla2set11e20tt5k9v9.png" alt="poll_phase" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once Node.js starts the program, the event loop processes all the queues and blocks on the poll phase waiting for incoming requests. When someone makes a GET HTTP request to the server, the event loop goes through these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The event loop pushes the request handler on the call stack.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;setImmediate&lt;/code&gt; callback is added in the &lt;strong&gt;check&lt;/strong&gt; phase.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;setTimeout&lt;/code&gt; callback is scheduled in the &lt;strong&gt;timers&lt;/strong&gt; phase.&lt;/li&gt;
&lt;li&gt;The event loop moves to the check phase. The &lt;code&gt;setImmediate&lt;/code&gt; callback is popped off from the queue and pushed on the call stack for execution.&lt;/li&gt;
&lt;li&gt;The event loop does not move any callbacks from empty queues.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;setTimeout&lt;/code&gt; callback is popped off from the timer queue and pushed on the call stack for processing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/772860074" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The code is available at:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/fabrilallo/animated-nodejs/tree/main/02-event-loop-phases" rel="noopener noreferrer"&gt;Animated Node.js-02-event-loop-phases repository&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now, you understand the event loop and its phases. You can also tell the difference between &lt;code&gt;setTimeout&lt;/code&gt; and &lt;code&gt;setImmediate&lt;/code&gt; and expect a non-deterministic behavior when running them in sequence. In addition, you know that event loop blocks on the poll phase waiting for new I/O requests on a Node.js web server.&lt;/p&gt;

&lt;p&gt;But where are promises handled in Node.js?&lt;/p&gt;

&lt;p&gt;You will explore it in more detail in the following articles.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/" rel="noopener noreferrer"&gt;Node.js official documentation: The Node.js Event Loop, Timers, and process.nextTick()&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/libuv/libuv/blob/v1.x/src/unix/core.c#L384" rel="noopener noreferrer"&gt;Libuv event loop source code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, follow us on Twitter &lt;a href="https://twitter.com/FabriLallo" rel="noopener noreferrer"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368" rel="noopener noreferrer"&gt;@AndrewHu368&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>What is Node.js?</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Sun, 30 Oct 2022 21:38:22 +0000</pubDate>
      <link>https://forem.com/nodedoctors/what-is-nodejs-3if2</link>
      <guid>https://forem.com/nodedoctors/what-is-nodejs-3if2</guid>
      <description>&lt;p&gt;JavaScript had the reputation of being a toy language. A tool to add interactivity to web pages. For a long time, its use cases were limited to the browser, but the creation of Node.js completely changed its role in the modern web development world.&lt;/p&gt;

&lt;p&gt;Node.js allows us to run JavaScript outside the browser, and out of nowhere, a limited language became the favorite companion for thousands of developers working on small and big projects.&lt;/p&gt;

&lt;p&gt;Its vibrant community created many tools that power many applications on servers, desktops, and embedded systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js is commonly used with Express or Fastify to power complex backend services.&lt;/li&gt;
&lt;li&gt;Electron package Node.js and Chrome to create cross-platform desktop applications. It is used in production to power programmers’ favorite editor VSCode and messaging applications like Microsoft Teams.&lt;/li&gt;
&lt;li&gt;Embedded systems can leverage low.js to develop applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Node.js?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Before Node.js
&lt;/h3&gt;

&lt;p&gt;Contrary to popular belief, Node wasn’t invented to allow JavaScript outside the browser. The latter was a positive outcome. It was developed to create more performant web servers.&lt;/p&gt;

&lt;p&gt;Ten years ago, handling multiple user connections involved the creation of a new thread, an independent unit of execution within the web server process. This methodology implied two problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you perform a database query, it blocks the entire thread from executing other operations until it receives the result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j7F2UlE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kjd6aumd2jb16gnzn34a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j7F2UlE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kjd6aumd2jb16gnzn34a.png" alt="blocking execution" width="880" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handling concurrent connections with associated threads requires high memory consumption and increased difficulty in managing synchronization between them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Node.js way
&lt;/h3&gt;

&lt;p&gt;JavaScript and Node.js solve the problem of threads' high memory usage and synchronization by using a single thread paired with an event loop mechanism. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gOwyK6km--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo7tgkn71flt5dadb0zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gOwyK6km--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lo7tgkn71flt5dadb0zj.png" alt="non-blocking execution" width="880" height="801"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code snippet above, Node.js can handle concurrent connections and run other pieces of code because the database query execution is delegated to the event loop. The database operation has a callback function which is run when the event loop notifies the single thread that the query is completed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BHOi4FCg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj5xy47mwvliwnq2o6o1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BHOi4FCg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj5xy47mwvliwnq2o6o1.png" alt="single thread running the query" width="880" height="1511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js internal overview
&lt;/h2&gt;

&lt;p&gt;To understand Node.js, we need to understand V8.&lt;/p&gt;

&lt;p&gt;V8 is a C++ program that runs JavaScript code and converts it into instructions understandable by the computer microprocessor. V8 is included in Google Chrome and other browsers to run our websites JavaScript and enable our rich web experience.&lt;/p&gt;

&lt;p&gt;The V8 engine is not limited to modern browsers but can also be embedded into any C++ program and extended with more functionalities. Not by chance, Node.js is a C++ program that integrates the V8 engine and other libraries to enrich JavaScript with new features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;libuv, the C++ library responsible for the event loop, TCP and UDP connection sockets, DNS resolution, file system operations, data encryption, etc.&lt;/li&gt;
&lt;li&gt;C++ bindings are responsible for invoking C++ code when running Node.js-specific JavaScript code.&lt;/li&gt;
&lt;li&gt;The JavaScript core API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--axyCAFpG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4z7ypdgiul0y3ddvt1p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--axyCAFpG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4z7ypdgiul0y3ddvt1p.png" alt="node.js internals overview" width="880" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Node.js clever design and event loop has empowered thousands of developers and products to create web servers capable of handling thousands of concurrent connections without the hassle of threads’ high memory consumption and synchronization. Node.js event loop capabilities are not limited to database queries and concurrent connections, but they are also used in production to process large files, encrypt/decrypt data, perform network requests, etc.&lt;/p&gt;

&lt;p&gt;You will explore in more detail the event loop in the following article.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, follow us on Twitter &lt;a href="https://twitter.com/FabriLallo"&gt;@fabrizio.lallo&lt;/a&gt; and &lt;a href="https://twitter.com/AndrewHu368"&gt;@AndrewHu368&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>ORM Database Fundamentals</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Tue, 18 Oct 2022 17:07:42 +0000</pubDate>
      <link>https://forem.com/fabriziolallo/database-orm-fundamentals-3jl</link>
      <guid>https://forem.com/fabriziolallo/database-orm-fundamentals-3jl</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is an ORM Database?&lt;/li&gt;
&lt;li&gt;Query Builder&lt;/li&gt;
&lt;li&gt;Identity Map&lt;/li&gt;
&lt;li&gt;Unit of Work&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is an ORM Database?
&lt;/h2&gt;

&lt;p&gt;An ORM Database (&lt;em&gt;Object Relational Mapping&lt;/em&gt;) is concerned with helping your application achieve persistence. Persistence simply means we would like our application’s data to outlive the application process. But, expressly, &lt;em&gt;Object Relational Mapping&lt;/em&gt; is a technique to achieve a  mapping between database data and objects used with your programming language.&lt;/p&gt;

&lt;p&gt;Object Relational Mapping achieves this goal through fundamental concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Query Builder&lt;/li&gt;
&lt;li&gt;Identity Map&lt;/li&gt;
&lt;li&gt;Unit of Work&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Query Builder
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Query Builder&lt;/strong&gt; provides an API designed for constructing a DQL (&lt;em&gt;Domain Query Language&lt;/em&gt;) query in several steps. It provides a set of classes and methods that can build queries programmatically. Usually, you can use Query Builder API to build a query quickly or just use the raw DQL language if you need something more complex that it’s easy to write in raw DQL language.&lt;/p&gt;

&lt;p&gt;So let’s imagine we need to execute the following &lt;code&gt;SELECT&lt;/code&gt; query in our application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Users&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Query Builder API to select rows in a table could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;orm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the time, people think this is the definition of ORM, but as we’ll see, two other fundamental concepts are essential for an ORM to be defined as such.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identity Map
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;Identity Map&lt;/strong&gt; keeps a record of all objects that have been read from the database in a single business transaction. Whenever you want an object, the ORM checks the Identity Map first to see if that object is already contained. So, it acts as a cache.&lt;/p&gt;

&lt;p&gt;Having an identity map is crucial for avoiding cases when you read the same database record in two different objects. In this case, when you update them both, you'll have a lousy time writing the changes out to the database correctly.&lt;/p&gt;

&lt;p&gt;Furthermore, reading the same record several times can cause an expensive cost in remote calls to the database. So, not loading the same data twice helps avoid profound problems writing changes to the database and speeds up your application.&lt;/p&gt;

&lt;p&gt;Let’s put it into practice with some javascript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Both queries return the same user&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;// identity map in action&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Unit of Work
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Unit of Work&lt;/strong&gt; keeps track of every change you make during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database due to your work.&lt;/p&gt;

&lt;p&gt;Keeping track of what you've changed when pulling data in and out of a database is fundamental. Otherwise, that data won't be written back into the database.&lt;/p&gt;

&lt;p&gt;You can change the database with each change to your object model, but this can lead to many very slow and very small database calls. Furthermore, it requires you to have a transaction open for the whole interaction, which is impractical if you have a business transaction that spans multiple requests.&lt;/p&gt;

&lt;p&gt;Let’s see what a unit of work looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;orm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;surname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// it will apply all the changes done in this unit of work&lt;/span&gt;
&lt;span class="c1"&gt;// so user1 and user2 age will be updated in the database&lt;/span&gt;
&lt;span class="c1"&gt;// using a single transaction&lt;/span&gt;
&lt;span class="nx"&gt;orm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usually, when there are a lot of changes, an ORM should take care to batch the changes in several transactions in order not to create a single heavy transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog post, we explored the fundamental concepts of a Database ORM: Query Builder, Identity Map and Unit of work.&lt;/p&gt;

&lt;p&gt;Certainly, an ORM has many features to make easy interaction with a database, but these are the main building blocks.&lt;/p&gt;

&lt;p&gt;Stay tuned for the next blog posts. 👀&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on Twitter if you enjoyed this blog post&lt;/em&gt; &lt;a href="https://twitter.com/FabriLallo"&gt;@fabri.lallo&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>5 tips to improve software development and release process</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Sun, 16 Oct 2022 17:47:07 +0000</pubDate>
      <link>https://forem.com/fabriziolallo/5-tips-to-improve-software-development-and-release-process-5ada</link>
      <guid>https://forem.com/fabriziolallo/5-tips-to-improve-software-development-and-release-process-5ada</guid>
      <description>&lt;p&gt;Every software engineering team follow specific development practices to build and release software.&lt;/p&gt;

&lt;p&gt;Here are 5 tips to improve the software development and release process.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Stop using pull requests and code reviews
&lt;/h2&gt;

&lt;p&gt;If you write tests, you don't need code reviews. Leave pull requests and code reviews where they belong so to open-source projects.&lt;/p&gt;

&lt;p&gt;You can still rely on disciplined pair programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  2) Use Trunk Based Development
&lt;/h2&gt;

&lt;p&gt;Trunk Based Development is a version control management practice where developers merge small, frequent updates to a core “trunk” or main branch.&lt;/p&gt;

&lt;p&gt;Stop leaving those branches open for days, weeks, or months. Stop making it hard to merge changes between branches.&lt;/p&gt;

&lt;p&gt;Push small changes at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  3) Write tests
&lt;/h2&gt;

&lt;p&gt;Writing tests is the most powerful weapon to protect your application from bugs.&lt;/p&gt;

&lt;p&gt;Cover every surface of your application through unit tests, e2e test etc..&lt;/p&gt;

&lt;h2&gt;
  
  
  4) Setup a CI/CD pipeline
&lt;/h2&gt;

&lt;p&gt;Let the CI/CD pipeline take care of building your application, executing tests and deploying it.&lt;/p&gt;

&lt;p&gt;Automate as much as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  5) Trust your team
&lt;/h2&gt;

&lt;p&gt;Last but not least, trust your team.&lt;/p&gt;

&lt;p&gt;Every team member should trust each other and feel autonomous.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on Twitter if you enjoyed this blog post&lt;/em&gt; &lt;a href="https://twitter.com/FabriLallo"&gt;@fabri.lallo&lt;/a&gt; &lt;/p&gt;

</description>
      <category>productivity</category>
      <category>programming</category>
      <category>career</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Moon phases with Node.js streams over HTTP</title>
      <dc:creator>Fabrizio Lallo</dc:creator>
      <pubDate>Thu, 13 Oct 2022 08:30:24 +0000</pubDate>
      <link>https://forem.com/fabriziolallo/moon-phases-with-nodejs-streams-over-http-1mmf</link>
      <guid>https://forem.com/fabriziolallo/moon-phases-with-nodejs-streams-over-http-1mmf</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to Node.js streams
&lt;/h2&gt;

&lt;p&gt;Streams are a fundamental building block of many Node.js functionalities. They provide a way to consume data as soon as it’s available and send the output as soon as the application produces it.&lt;br&gt;
Just think about data-intensive applications which can’t afford to accumulate all data in memory and process them all at once. In this case, applications must chunk data not to overflow the memory, which is what streams do.&lt;/p&gt;

&lt;p&gt;A crucial example of stream usage in Node.js is the HTTP server.&lt;br&gt;
HTTP server use streams in order never to buffer entire requests or responses so that the user can stream chunked data.&lt;/p&gt;

&lt;p&gt;There are four fundamental stream types within Node.js:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Writable&lt;/strong&gt;: streams to which data can be written&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Readable&lt;/strong&gt;: streams from which data can be consumed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Duplex&lt;/strong&gt;: streams that are both &lt;code&gt;Readable&lt;/code&gt; and &lt;code&gt;Writable&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transform&lt;/strong&gt;: &lt;code&gt;Duplex&lt;/code&gt; streams that can transform the data as it is written and read&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this first blog post, we will explore &lt;strong&gt;Readable&lt;/strong&gt; and &lt;strong&gt;Writable&lt;/strong&gt; streams reproducing moon phases over an HTTP server.&lt;/p&gt;

&lt;p&gt;🌕 🌖 🌗 🌘 🌑🌘🌗🌖🌕&lt;/p&gt;
&lt;h2&gt;
  
  
  Let’s code
&lt;/h2&gt;

&lt;p&gt;The goal is to create a simple server which continuously shows moon phases with emojis. So we need to define two streams:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;Readable&lt;/strong&gt; stream from which we can read the emojis&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Writable&lt;/strong&gt; stream which is represented by the HTTP server response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start by initializing the project and installing the required dependencies.&lt;br&gt;
For this project, I'm using &lt;strong&gt;Node.js&lt;/strong&gt; &lt;code&gt;v16.17.1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;

npm &lt;span class="nb"&gt;install &lt;/span&gt;node-emoji
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The library &lt;code&gt;node-emoji&lt;/code&gt; adds emojis support to Node.js&lt;/p&gt;

&lt;p&gt;We need only one file, so let's create an &lt;code&gt;index.js&lt;/code&gt; file. Firstly, we focus on building the &lt;strong&gt;Readable&lt;/strong&gt; stream, which is the core task of this project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoonPhasesStream&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Readable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:full_moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:waning_gibbous_moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:last_quarter_moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:waning_crescent_moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:new_moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:waxing_crescent_moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:first_quarter_moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;emoji&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;:moon:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;_read &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LAST_MOON_PHASE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FIRST_MOON_PHASE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moonPhases&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;LAST_MOON_PHASE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;FIRST_MOON_PHASE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've just created a custom &lt;strong&gt;Readable&lt;/strong&gt; stream; wow.&lt;/p&gt;

&lt;p&gt;Let's break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First thing first, to define a custom &lt;strong&gt;Readable&lt;/strong&gt; stream, we need to create a class that extends the &lt;strong&gt;Readable&lt;/strong&gt; stream base class&lt;/li&gt;
&lt;li&gt;In the constructor, we initialize an empty array and fill it with the eight moon phases emojis. We also need an index to restart the moon phases when it ends.&lt;/li&gt;
&lt;li&gt;Now we can override the &lt;code&gt;_read()&lt;/code&gt; method, which is responsible for producing data (emojis in this case). In this method, we just 'produce' data using the &lt;code&gt;push&lt;/code&gt; method of the &lt;strong&gt;Readable&lt;/strong&gt; stream. When the moon phases flow is over, we restart it setting the index again to 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, so now that we have our &lt;code&gt;MoonPhasesStream&lt;/code&gt;, it's time to define the consumer of these emojis streams.&lt;br&gt;
Let's define our &lt;strong&gt;Writable&lt;/strong&gt; stream using the Node.js HTTP server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;moonPhasesStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MoonPhasesStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nf"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;moonPhasesStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Very simple, no?&lt;/p&gt;

&lt;p&gt;Let's explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In Node.js, the &lt;code&gt;request&lt;/code&gt; object is a &lt;strong&gt;Readable&lt;/strong&gt; stream so that we can consume data from it. But the object we are interested in is the &lt;code&gt;response&lt;/code&gt; which is the Writable stream on which the emoji will flow. So, we won't need to build a custom &lt;strong&gt;Writable&lt;/strong&gt; stream as we did for the Readable one.&lt;/li&gt;
&lt;li&gt;There are several ways to combine streams, and the most used are &lt;code&gt;pipe()&lt;/code&gt;and &lt;code&gt;pipeline()&lt;/code&gt;. The difference is that with &lt;code&gt;pipe()&lt;/code&gt; if there's an error, we need to end and destroy each stream (to avoid memory leaks) manually:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;readableFileStream&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;readableFileStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decompressStream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;decompressStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decryptStream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;decryptStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;pipeline()&lt;/code&gt; solve this problem by taking care of the stream destruction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;readableFileStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;decompressStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;decryptStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Process completed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's why we also use &lt;code&gt;pipeline()&lt;/code&gt; to compose &lt;strong&gt;Readable&lt;/strong&gt; and &lt;strong&gt;Writable&lt;/strong&gt; streams:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;moonPhasesStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MoonPhasesStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nf"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;moonPhasesStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we're ready to try our server. Just start it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then open a terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see an infinite moon phases cycle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2jd4nmvgtajbf3orr5l.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2jd4nmvgtajbf3orr5l.gif" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2jd4nmvgtajbf3orr5l.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to show just one moon phases cycle, it's possible to stop the stream by pushing a &lt;code&gt;null&lt;/code&gt; value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this first blog post, we created a custom &lt;strong&gt;Readable&lt;/strong&gt; stream to produce data and used the HTTP server response, a &lt;strong&gt;Writable&lt;/strong&gt; stream, to consume them.&lt;/p&gt;

&lt;p&gt;Here is my GitHub repository: &lt;a href="https://github.com/fabrilallo/moon-phases-stream" rel="noopener noreferrer"&gt;https://github.com/fabrilallo/moon-phases-stream&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stay tuned for the next blog posts. 👀&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Follow me on Twitter if you enjoyed this blog post&lt;/em&gt; &lt;a href="https://twitter.com/FabriLallo" rel="noopener noreferrer"&gt;@fabri.lallo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
