<?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: Charles Yun</title>
    <description>The latest articles on DEV Community by Charles Yun (@charles_yun_676b13b5c3355).</description>
    <link>https://dev.to/charles_yun_676b13b5c3355</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4196318%2F7ed060ba-64b3-4e2c-b5a2-c7bbb3665ea0.png</url>
      <title>DEV Community: Charles Yun</title>
      <link>https://dev.to/charles_yun_676b13b5c3355</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charles_yun_676b13b5c3355"/>
    <language>en</language>
    <item>
      <title>Lessons Learned Building a Multi-Platform Publishing Tool with Fastify</title>
      <dc:creator>Charles Yun</dc:creator>
      <pubDate>Tue, 20 Oct 2026 15:54:38 +0000</pubDate>
      <link>https://dev.to/charles_yun_676b13b5c3355/lessons-learned-building-a-multi-platform-publishing-tool-with-fastify-2l9b</link>
      <guid>https://dev.to/charles_yun_676b13b5c3355/lessons-learned-building-a-multi-platform-publishing-tool-with-fastify-2l9b</guid>
      <description>&lt;p&gt;I've been building a publishing tool , and I chose Fastify for the backend. The reasoning was simple: official plugins (jwt, cookie, cors) cover all the essentials, the performance reputation is solid, and TypeScript support is complete. Now that the P0 end-to-end flow is running, here are the pitfalls I hit along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The second argument to &lt;code&gt;register&lt;/code&gt; is not what you think
&lt;/h2&gt;

&lt;p&gt;The most subtle pitfall. I wanted to protect a whole group of business routes with authentication, so I naturally wrote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;connectionRoutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;preHandler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authenticate&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;The result: every endpoint was wide open — unauthenticated requests got through. Digging into the docs revealed that the second options object of &lt;code&gt;register&lt;/code&gt; is &lt;strong&gt;passed as an argument to the plugin function&lt;/strong&gt;, not treated as route config. The &lt;code&gt;preHandler&lt;/code&gt; simply never gets applied.&lt;/p&gt;

&lt;p&gt;The correct approach is to attach the hook inside the plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&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;connectionRoutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FastifyInstance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addHook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;preHandler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&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;code&gt;addHook&lt;/code&gt; follows Fastify's encapsulation model and only affects routes registered within that plugin — which is exactly the "authenticate this whole group" behavior I wanted.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The request keeps running after &lt;code&gt;reply.send()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The second pitfall is more dangerous. My first version of the auth decorator looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;jwtVerify&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&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="nf"&gt;code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Unauthorized&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;After &lt;code&gt;reply.send()&lt;/code&gt;, the request chain &lt;strong&gt;does not stop&lt;/strong&gt;. The route handler still executes. The client receives a 401, but the business logic inside the handler has already run — for write operations, that's an incident waiting to happen.&lt;/p&gt;

&lt;p&gt;The fix: &lt;code&gt;throw&lt;/code&gt; directly inside the decorator and let &lt;code&gt;setErrorHandler&lt;/code&gt; handle it centrally. Once an error is thrown in Fastify, the remaining hooks and the handler are skipped — which is the behavior you'd intuitively expect.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. setErrorHandler is the perfect funnel
&lt;/h2&gt;

&lt;p&gt;Together with pitfall #2, I centralized all error handling in a single &lt;code&gt;setErrorHandler&lt;/code&gt;: custom &lt;code&gt;HttpError&lt;/code&gt;s pass their status code through, errors thrown by &lt;code&gt;jwtVerify&lt;/code&gt; get a unified message, and unknown errors get logged and return a 500. Route handlers now only ever do &lt;code&gt;throw new HttpError(404, 'Post not found')&lt;/code&gt; — no more try/catch noise.&lt;/p&gt;

&lt;p&gt;One type-related note: &lt;code&gt;@fastify/jwt&lt;/code&gt; requires manual module augmentation (declaring the &lt;code&gt;FastifyJWT&lt;/code&gt; interface). A few lines of &lt;code&gt;declare module&lt;/code&gt; give &lt;code&gt;req.user&lt;/code&gt; proper types — an easy page to miss in the docs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;p&gt;Fastify is quick to pick up and genuinely fast, but its &lt;strong&gt;plugin encapsulation model&lt;/strong&gt; is a different mental model from Express-style "global middleware". Once you understand the scope boundaries, the first two pitfalls never happen. If your next Node project needs a high-performance API with a first-class TypeScript experience, Fastify is an easy recommendation.&lt;/p&gt;

</description>
      <category>fastify</category>
      <category>node</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
