<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Efaz on Medium]]></title>
        <description><![CDATA[Stories by Efaz on Medium]]></description>
        <link>https://medium.com/@efazkh?source=rss-6f948b763b54------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KjGV4Z5-V899D7-D</url>
            <title>Stories by Efaz on Medium</title>
            <link>https://medium.com/@efazkh?source=rss-6f948b763b54------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 01:24:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@efazkh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Guards vs Middleware in NestJS]]></title>
            <link>https://medium.com/@efazkh/guards-vs-middleware-in-nestjs-352c574ae80b?source=rss-6f948b763b54------2</link>
            <guid isPermaLink="false">https://medium.com/p/352c574ae80b</guid>
            <dc:creator><![CDATA[Efaz]]></dc:creator>
            <pubDate>Fri, 15 May 2026 03:49:41 GMT</pubDate>
            <atom:updated>2026-05-15T03:49:41.205Z</atom:updated>
            <content:encoded><![CDATA[<p>When building scalable backend applications with NestJS, developers often get confused between <strong>Middleware</strong> and <strong>Guards</strong> because both can intercept requests before they reach the controller. However, they are built with different purposes and philosophies in mind.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*fqdbsPujnT5h3OrK-OpZvg.png" /></figure><p>One important distinction is:</p><ul><li><strong>Middleware</strong> comes from the traditional Express.js ecosystem</li><li><strong>Guards</strong> are a core feature designed specifically by NestJS</li></ul><blockquote><strong>What is Middleware?</strong></blockquote><p>Middleware is inherited from the Express.js architecture that NestJS uses under the hood by default.</p><p>It runs before the route handler and is mainly used for request preprocessing.</p><p>Common use cases include:</p><ul><li>Logging requests</li><li>Parsing tokens</li><li>Adding headers</li><li>Request transformation</li><li>Basic validation</li></ul><pre>@Injectable()<br>export class LoggerMiddleware implements NestMiddleware {<br>  use(req: Request, res: Response, next: NextFunction) {<br>    console.log(`${req.method} ${req.originalUrl}`);<br>    next();<br>  }<br>}</pre><p>Middleware is generic and framework-agnostic. It does not know which controller or handler will execute next.</p><blockquote><strong>What is a Guard?</strong></blockquote><p>Guards are a native feature of NestJS and are built specifically for authorization and route protection.</p><p>They run after middleware and before the controller.</p><p>Guards are commonly used for:</p><ul><li>Authentication</li><li>Role-based access</li><li>Permission checking</li><li>JWT authorization</li></ul><pre>@Injectable()<br>export class AuthGuard implements CanActivate {<br>  canActivate(context: ExecutionContext): boolean {<br>    const request = context.switchToHttp().getRequest();<br>    return !!request.headers.authorization;<br>  }<br>}</pre><p>Unlike middleware, Guards have access to NestJS’s ExecutionContext, meaning they understand exactly which route and handler are being called.</p><h3>When Should You Use Which?</h3><p>Use <strong>Middleware</strong> when handling generic request-level operations.</p><p>Use <strong>Guards</strong> when protecting routes or validating user access.</p><p>A simple way to remember it:</p><blockquote><strong>Middleware processes the request, while Guards protect the route.</strong></blockquote><p>Both Middleware and Guards are essential parts of the backend architecture in NestJS.</p><p>Middleware brings the flexibility of Express.js, while Guards provide a structured and scalable authorization system designed by NestJS itself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=352c574ae80b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NestJS Lifecycle Hooks Explained Simply]]></title>
            <link>https://medium.com/@efazkh/nestjs-lifecycle-hooks-explained-simply-e53b34579fed?source=rss-6f948b763b54------2</link>
            <guid isPermaLink="false">https://medium.com/p/e53b34579fed</guid>
            <category><![CDATA[lifecycle-methods]]></category>
            <category><![CDATA[special-methods]]></category>
            <category><![CDATA[lifecycle-events]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Efaz]]></dc:creator>
            <pubDate>Mon, 11 May 2026 14:35:26 GMT</pubDate>
            <atom:updated>2026-05-11T14:35:26.949Z</atom:updated>
            <content:encoded><![CDATA[<p>While working with NestJS, developers often need to run certain tasks when the application starts or stops. This is where <strong>Lifecycle Hooks</strong> become very useful.</p><p>Lifecycle hooks help manage different phases of an application in a clean and organized way. Instead of placing startup or cleanup logic randomly inside services, NestJS provides dedicated methods for handling them.</p><p>Also known as Special Methods, these are lifecycle events or methods.</p><p>For example, you might want to:</p><ul><li>Connect to a database when the app starts</li><li>Load configuration data</li><li>Start scheduled jobs</li><li>Clear resources before shutdown</li></ul><p>NestJS provides built-in hooks for these situations.</p><p>One commonly used hook is OnModuleInit, which runs when a module is initialized.</p><pre>import { Injectable, OnModuleInit } from &#39;@nestjs/common&#39;;<br><br>@Injectable()<br>export class UserService implements OnModuleInit {<br>  onModuleInit() {<br>    console.log(&#39;Module Initialized&#39;);<br>  }<br>}</pre><p>Another useful hook is OnApplicationBootstrap, which executes after the entire application has started successfully.</p><pre>import { Injectable, OnApplicationBootstrap } from &#39;@nestjs/common&#39;;<br><br>@Injectable()<br>export class AppService implements OnApplicationBootstrap {<br>  onApplicationBootstrap() {<br>    console.log(&#39;Application Started&#39;);<br>  }<br>}</pre><p>For cleanup operations, NestJS provides OnModuleDestroy.</p><pre>import { Injectable, OnModuleDestroy } from &#39;@nestjs/common&#39;;<br><br>@Injectable()<br>export class AppService implements OnModuleDestroy {<br>  onModuleDestroy() {<br>    console.log(&#39;Cleaning up resources...&#39;);<br>  }<br>}</pre><p>Lifecycle hooks are especially helpful in large-scale backend applications because they improve code organization and application maintainability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BlPYV0xVY637kyWxBSy5xQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e53b34579fed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>