<?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>DEV Community: Dymme</title>
    <description>The latest articles on DEV Community by Dymme (@dymmepay).</description>
    <link>https://dev.to/dymmepay</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%2Forganization%2Fprofile_image%2F7527%2F38cfb269-6bbd-4011-91d9-4e0beb03b0d0.png</url>
      <title>DEV Community: Dymme</title>
      <link>https://dev.to/dymmepay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dymmepay"/>
    <language>en</language>
    <item>
      <title>From zero to production with Fastify</title>
      <dc:creator>Christopher Ribeiro</dc:creator>
      <pubDate>Tue, 12 Sep 2023 13:00:00 +0000</pubDate>
      <link>https://dev.to/dymmepay/from-zero-to-production-with-fastify-574m</link>
      <guid>https://dev.to/dymmepay/from-zero-to-production-with-fastify-574m</guid>
      <description>&lt;p&gt;When developing an application we often ask ourselves&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Which framework should I use?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We know this question actually comes a lot in the JavaScript ecosystem.&lt;/p&gt;

&lt;p&gt;In this post I show why at &lt;a href="https://dymme.com" rel="noopener noreferrer"&gt;Dymme&lt;/a&gt; we chose Fastify as our back-end framework and how we use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The why
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Fast by default:&lt;/strong&gt;&lt;br&gt;
Fastify kills at every benchmark &lt;a href="https://fastify.dev/benchmarks/" rel="noopener noreferrer"&gt;see&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Express like:&lt;/strong&gt;&lt;br&gt;
We can write code just like in the ol' Express days if we want to.&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="nx"&gt;fastify&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;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;world&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;&lt;strong&gt;3. Configurable:&lt;/strong&gt;&lt;br&gt;
Fastify offers a plugin system just like Express. Plugins can modify Fastify or act in certain moments (when the server closes or the request is sent for example).&lt;br&gt;
The chosen approach makes us write decoupled code.&lt;/p&gt;

&lt;p&gt;Each plugin is independent. Meaning that they can't talk to each other directly.&lt;br&gt;
All plugins talk to the Fastify instance directly, which itself is a plugin. And if a plugin wants to communicate with another it communicates via the Fastify instance.&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="nx"&gt;fastify&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decorate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;db&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DbConnection&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="nx"&gt;fastify&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;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;await&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;world&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See that, we make the &lt;code&gt;db&lt;/code&gt; plugin available for the whole instance. This makes possible to use this plugin directly from anywhere that has access to the Fastify instance.&lt;br&gt;
In this case, our route handler has the Fastify instance by default and we can access it to call our &lt;code&gt;db&lt;/code&gt; plugin without direct access to it.&lt;/p&gt;

&lt;p&gt;And if you followed along, you may note that a route is also a plugin 🤯 that's how it can talk to the Fastify instance and use the &lt;code&gt;db&lt;/code&gt; plugin added to it.&lt;/p&gt;

&lt;p&gt;Take a look in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fastify.dev/ecosystem/" rel="noopener noreferrer"&gt;Ecosystem&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fastify.dev/docs/latest/Guides/Getting-Started#your-first-plugin" rel="noopener noreferrer"&gt;Your first plugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fastify.dev/docs/latest/Reference/Hooks" rel="noopener noreferrer"&gt;Hooks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. TypeScript:
&lt;/h3&gt;

&lt;p&gt;Yes it supports TypeScript, so you can have autocomplete (and make some guy want to remove it from your codebase).&lt;/p&gt;

&lt;h2&gt;
  
  
  The how
&lt;/h2&gt;

&lt;p&gt;By now I may have convinced you that Fastify is the best Express alternative than others. But what about how we do it at &lt;a href="https://dymme.com" rel="noopener noreferrer"&gt;Dymme&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;Glad you asked!&lt;/p&gt;

&lt;p&gt;Dymme is a Startup that enables merchants to sell their products in one click and shoppers to pay also in one click, instantly. As we deal with real time payments, we need to have an API that handles heavy traffic and be able to move quickly.&lt;/p&gt;

&lt;p&gt;Fastify allows all that but if you start from scratch you may not perceive the difference from Express.&lt;br&gt;
That's why we recommend you to start your application with the &lt;a href="https://github.com/fastify/fastify-cli" rel="noopener noreferrer"&gt;CLI&lt;/a&gt;.&lt;br&gt;
It will not only bootstrap you application but will configure it to have TypeScript if you want.&lt;/p&gt;

&lt;p&gt;The CLI will also setup your API to use filesystem routing (Yes, you heard that).&lt;/p&gt;

&lt;p&gt;Take a look in &lt;a href="https://github.com/ChristoPy/fastify-template" rel="noopener noreferrer"&gt;this base template&lt;/a&gt; we are using internally.&lt;br&gt;
We will update it in the future with better patterns, so stay tuned and give it a star!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://dymme.com" rel="noopener noreferrer"&gt;Dymme&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>typescript</category>
      <category>bunjs</category>
    </item>
  </channel>
</rss>
