<?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: pwnkdm</title>
    <description>The latest articles on DEV Community by pwnkdm (@pwnkdm).</description>
    <link>https://dev.to/pwnkdm</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%2F1975508%2F628cf817-f8c5-4e00-b0f9-81df422ef004.png</url>
      <title>DEV Community: pwnkdm</title>
      <link>https://dev.to/pwnkdm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pwnkdm"/>
    <language>en</language>
    <item>
      <title>Building IIT Ropar’s Digital Future: The Story of Two Major Launches</title>
      <dc:creator>pwnkdm</dc:creator>
      <pubDate>Mon, 21 Jul 2025 13:23:34 +0000</pubDate>
      <link>https://dev.to/pwnkdm/building-iit-ropars-digital-future-the-story-of-two-major-launches-ee3</link>
      <guid>https://dev.to/pwnkdm/building-iit-ropars-digital-future-the-story-of-two-major-launches-ee3</guid>
      <description>&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%2Fhekcu8qksosyh7w08o1v.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%2Fhekcu8qksosyh7w08o1v.png" alt=" " width="800" height="380"&gt;&lt;/a&gt;&lt;br&gt;
One Month, Two Platforms, and a Milestone I’ll Always Remember&lt;/p&gt;

&lt;p&gt;Over the past few weeks, I had the opportunity to design and develop two important platforms for IIT Ropar: the CDPC (Career Development and Placement Centre) and the CAPS (Corporate, Alumni, Placement and Strategies) websites. Both platforms were launched on 16th July 2025 during the institute’s 14th Annual Convocation. It was a proud and unforgettable milestone in my journey.&lt;/p&gt;

&lt;p&gt;As a developer, there is nothing more fulfilling than seeing your work go live. But watching it launch during a prestigious academic ceremony, in the presence of:&lt;/p&gt;

&lt;p&gt;Mr. Shrikant M Vaidya, Former Chairman, Indian Oil Corporation Ltd (Chief Guest)&lt;/p&gt;

&lt;p&gt;Mr. Adil Zainulbhai, Chairperson, Board of Governors, IIT Ropar&lt;/p&gt;

&lt;p&gt;Prof. Rajeev Ahuja, Director, IIT Ropar&lt;/p&gt;

&lt;p&gt;Prof. Pushpendra P. Singh, Dean, CAPS&lt;/p&gt;

&lt;p&gt;made the experience even more special.&lt;/p&gt;

&lt;p&gt;My Role in the Project&lt;br&gt;
I built both platforms from the ground up using the MERN stack, handling everything from front-end design to back-end logic and deployment. This end-to-end development journey provided valuable insight into how institutional platforms are envisioned, designed, and delivered while meeting real-world expectations.&lt;/p&gt;

&lt;p&gt;What These Platforms Represent&lt;br&gt;
CDPC – Career Development and Placement Centre&lt;br&gt;
A dedicated platform for student–industry engagement, mentoring, career readiness, and placement coordination.&lt;br&gt;
👉 &lt;a href="https://placements.iitrpr.ac.in/" rel="noopener noreferrer"&gt;Visit CDPC Website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CAPS – Corporate, Alumni, Placement and Strategies&lt;br&gt;
A collaborative interface focused on corporate engagement, alumni relations, student empowerment, and strategic partnerships.&lt;br&gt;
👉 &lt;a href="https://caps.iitrpr.ac.in/" rel="noopener noreferrer"&gt;Visit CAPS Website&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These platforms are more than just websites. They are tools that connect students with opportunities, ideas with industry, and alumni with meaningful engagement.&lt;/p&gt;

&lt;p&gt;Looking Ahead&lt;br&gt;
Completing this project on a tight timeline, collaborating with faculty and administrators, and delivering functional, scalable platforms has been a defining experience for me. It has sharpened my skills and expanded my understanding of building impactful digital solutions.&lt;/p&gt;

&lt;p&gt;I am truly grateful for the opportunity to contribute to IIT Ropar and look forward to the continued impact these platforms will create.&lt;/p&gt;

&lt;p&gt;More projects and experiences are on the horizon.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>mern</category>
      <category>react</category>
    </item>
    <item>
      <title>Fun with JavaScript: Changing Page Titles Dynamically</title>
      <dc:creator>pwnkdm</dc:creator>
      <pubDate>Thu, 03 Apr 2025 16:32:22 +0000</pubDate>
      <link>https://dev.to/pwnkdm/fun-with-javascript-changing-page-titles-dynamically-2gcg</link>
      <guid>https://dev.to/pwnkdm/fun-with-javascript-changing-page-titles-dynamically-2gcg</guid>
      <description>&lt;p&gt;Have you ever noticed how some websites change their page title when you switch tabs? It's a simple yet clever trick to grab a user's attention. Let’s explore how you can achieve this effect using just a few lines of JavaScript.&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%2Faz8e13qrcnvxhq6arw88.jpg" 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%2Faz8e13qrcnvxhq6arw88.jpg" alt="When tab changed tab title changed to " width="395" height="45"&gt;&lt;/a&gt;&lt;br&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%2Fei4shr5ox0y71ph1ppbm.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%2Fei4shr5ox0y71ph1ppbm.png" alt="When tab is in visible state title changed to " width="386" height="37"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Magic Behind &lt;code&gt;visibilitychange&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;visibilitychange&lt;/code&gt; event in JavaScript fires whenever a user switches between tabs or minimizes the browser window. Using this, we can dynamically update the document title based on the tab’s visibility state.&lt;/p&gt;

&lt;p&gt;Here's the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

document.addEventListener("visibilitychange", function () {
    if (document.hidden) {
      document.title = "I'm a lonely tab 😢";
    } else {
      document.title = "Welcome back 😊";
    }
  });


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The document.addEventListener listens for the "visibilitychange" event.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the tab is hidden (document.hidden is true), the title changes to "I'm a lonely tab 😢".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the user returns to the tab, the title is restored to "Welcome back 😊".&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why Use This?
&lt;/h2&gt;

&lt;p&gt;Engagement: This trick can remind users to return to your site.&lt;/p&gt;

&lt;p&gt;Branding: Custom messages can enhance your site's personality.&lt;/p&gt;

&lt;p&gt;User Experience: Fun, interactive elements make websites more enjoyable.&lt;/p&gt;

&lt;p&gt;Try adding this snippet to your website and see how it works!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Fixing Routing Issues in Vite React App on Vercel</title>
      <dc:creator>pwnkdm</dc:creator>
      <pubDate>Sun, 09 Mar 2025 18:35:00 +0000</pubDate>
      <link>https://dev.to/pwnkdm/fixing-routing-issues-in-vite-react-app-on-vercel-1o49</link>
      <guid>https://dev.to/pwnkdm/fixing-routing-issues-in-vite-react-app-on-vercel-1o49</guid>
      <description>&lt;p&gt;Deploying a &lt;strong&gt;Vite React app&lt;/strong&gt; on &lt;strong&gt;Vercel&lt;/strong&gt; is straightforward, but many developers face a common issue: &lt;strong&gt;navigating to another page and refreshing results in a 404 error&lt;/strong&gt;. This happens because Vercel serves only &lt;code&gt;index.html&lt;/code&gt; and doesn't recognize client-side routing handled by React Router.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When using &lt;strong&gt;React Router&lt;/strong&gt;, routes are managed on the client-side. However, Vercel doesn’t know how to handle these routes after a refresh, leading to a &lt;strong&gt;404 error&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Solution: Configure &lt;code&gt;vercel.json&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To fix this, create a &lt;code&gt;vercel.json&lt;/code&gt; file inside your frontend root directory (same level as &lt;code&gt;index.html&lt;/code&gt;) and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"rewrites"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/(.*)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"destination"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/index.html"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Explanation of &lt;code&gt;vercel.json&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rewrites&lt;/code&gt;&lt;/strong&gt;: Defines URL rewriting rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;source&lt;/code&gt;&lt;/strong&gt;: &lt;code&gt;"/(.*)"&lt;/code&gt; captures all routes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;destination&lt;/code&gt;&lt;/strong&gt;: Redirects all requests to &lt;code&gt;index.html&lt;/code&gt;, allowing React Router to handle routing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Deploy Again&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After adding &lt;code&gt;vercel.json&lt;/code&gt;, redeploy your Vite app using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vercel &lt;span class="nt"&gt;--prod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, navigating and refreshing pages will work without 404 errors. 🎉&lt;/p&gt;

&lt;p&gt;This simple fix ensures your Vite React app works smoothly with &lt;strong&gt;React Router on Vercel&lt;/strong&gt;. Happy coding! 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Ultimate Guide to Deploying a Node.js Express MongoDB Backend on Vercel</title>
      <dc:creator>pwnkdm</dc:creator>
      <pubDate>Sun, 09 Mar 2025 04:02:31 +0000</pubDate>
      <link>https://dev.to/pwnkdm/the-ultimate-guide-to-deploying-a-nodejs-express-mongodb-backend-on-vercel-lia</link>
      <guid>https://dev.to/pwnkdm/the-ultimate-guide-to-deploying-a-nodejs-express-mongodb-backend-on-vercel-lia</guid>
      <description>&lt;p&gt;Deploying a &lt;strong&gt;Node.js + Express + MongoDB&lt;/strong&gt; backend on &lt;strong&gt;Vercel&lt;/strong&gt; can sometimes be tricky, especially when dealing with different folder structures. This guide provides a &lt;strong&gt;detailed breakdown&lt;/strong&gt;, with a special focus on configuring the &lt;code&gt;vercel.json&lt;/code&gt; file correctly to avoid deployment errors.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before we begin, make sure you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node.js&lt;/strong&gt; installed on your local machine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MongoDB Atlas&lt;/strong&gt; account (or any cloud MongoDB instance).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub/GitLab/Bitbucket&lt;/strong&gt; account for repository hosting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vercel account&lt;/strong&gt; (sign up at &lt;a href="https://vercel.com" rel="noopener noreferrer"&gt;vercel.com&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Step 1: Set Up Your Express.js Project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you don’t have an existing project, create a new one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;express-vercel-backend &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;express-vercel-backend
npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install required dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express mongoose dotenv cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an &lt;code&gt;index.js&lt;/code&gt; file and set up a basic Express 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;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&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;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongoose&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;dotenv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dotenv&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;cors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;dotenv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;config&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="c1"&gt;// Connect to MongoDB&lt;/span&gt;
&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGO_URI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;useUnifiedTopology&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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="nf"&gt;then&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;MongoDB Connected&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="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;err&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;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;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="nx"&gt;res&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello from Express!&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;5000&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;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&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="s2"&gt;`Server running on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Step 2: Configure MongoDB Atlas&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Sign up at &lt;a href="https://www.mongodb.com/atlas" rel="noopener noreferrer"&gt;MongoDB Atlas&lt;/a&gt; and create a &lt;strong&gt;free cluster&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the database section, click &lt;strong&gt;Connect &amp;gt; Connect your application&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Copy the provided connection string and replace &lt;code&gt;&amp;lt;password&amp;gt;&lt;/code&gt; with your actual password.&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in your project root and add:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;MONGO_URI&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;mongodb+srv://your-username:your-password@cluster.mongodb.net/your-database-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Step 3: Understanding and Configuring &lt;code&gt;vercel.json&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Common Deployment Errors Due to Folder Structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Different projects have different folder structures, and incorrect configurations in &lt;code&gt;vercel.json&lt;/code&gt; can cause deployment failures. Based on the image provided, your backend folder structure is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MY-APP/
├── backend/
│   ├── controllers/
│   ├── email/
│   ├── middlewares/
│   ├── models/
│   ├── routes/
│   ├── .env
│   ├── index.js
│   ├── package.json
│   ├── package-lock.json
│   ├── vercel.json
├── frontend/
├── README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Updated &lt;code&gt;vercel.json&lt;/code&gt; Configuration&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Based on this structure, your &lt;code&gt;vercel.json&lt;/code&gt; should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Specifies&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Vercel&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;configuration&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;version.&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"builds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Defines&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;entry&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;point&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;backend.&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"use"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"@vercel/node"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"routes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/routes/(.*)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Routes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;requests&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;that&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;start&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;with&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'/routes'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;index.js.&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"dest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/(.*)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Routes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;all&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;requests&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;index.js.&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"dest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Key Differences from a Basic &lt;code&gt;vercel.json&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Configuration&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Basic Version&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Improved Version (Your Project)&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Routes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;{ "src": "/(.*)", "dest": "index.js" }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;{ "src": "/routes/(.*)", "dest": "index.js" }&lt;/code&gt; + &lt;code&gt;{ "src": "/(.*)", "dest": "index.js" }&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Purpose&lt;/td&gt;
&lt;td&gt;Routes all requests to &lt;code&gt;index.js&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Routes &lt;code&gt;/routes/...&lt;/code&gt; requests separately to ensure correct handling&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Folder Handling&lt;/td&gt;
&lt;td&gt;Assumes all endpoints are in &lt;code&gt;index.js&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Supports organized project structures with &lt;code&gt;/routes/&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This ensures that APIs under &lt;code&gt;/routes&lt;/code&gt; (like &lt;code&gt;/routes/users&lt;/code&gt; or &lt;code&gt;/routes/expenses&lt;/code&gt;) are correctly mapped to &lt;code&gt;index.js&lt;/code&gt; in your backend.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Step 4: Modify package.json for Vercel&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Vercel automatically detects frontend frameworks like React and Next.js, but for backend apps, we need to configure it explicitly.&lt;/p&gt;

&lt;p&gt;Modify your &lt;code&gt;package.json&lt;/code&gt; to include a &lt;code&gt;vercel-start&lt;/code&gt; script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;script&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;run&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;the&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;locally.&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"vercel-start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node index.js"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Custom&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;script&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Vercel&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;deployment.&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Step 5: Deploy on Vercel&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Install Vercel CLI&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; vercel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;2. Login to Vercel&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vercel login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;3. Initialize Vercel&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Run the following command and follow the prompts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vercel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;4. Set Environment Variables&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Go to &lt;strong&gt;Vercel Dashboard&lt;/strong&gt; &amp;gt; &lt;strong&gt;Project Settings&lt;/strong&gt; &amp;gt; &lt;strong&gt;Environment Variables&lt;/strong&gt; and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGO_URI=mongodb+srv://your-username:your-password@cluster.mongodb.net/your-database-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then redeploy your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vercel &lt;span class="nt"&gt;--prod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Step 6: Test Your Deployment&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Once deployed, Vercel will provide a live URL (e.g., &lt;code&gt;https://express-vercel-backend.vercel.app&lt;/code&gt;). Test your API by visiting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;https://your-vercel-url.com/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or using &lt;strong&gt;Postman&lt;/strong&gt; or &lt;code&gt;curl&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;curl https://your-vercel-url.com/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;You have successfully deployed a &lt;strong&gt;Node.js Express MongoDB backend&lt;/strong&gt; on &lt;strong&gt;Vercel&lt;/strong&gt;! 🎉&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Key Takeaways:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Folder structure plays a crucial role in deployment success.&lt;/li&gt;
&lt;li&gt;Configuring &lt;code&gt;vercel.json&lt;/code&gt; correctly avoids routing errors.&lt;/li&gt;
&lt;li&gt;Environment variables must be set manually in Vercel for secure database connections.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Next Steps:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Implement authentication (e.g., JWT).&lt;/li&gt;
&lt;li&gt;Connect this backend with a frontend (React, Next.js, etc.).&lt;/li&gt;
&lt;li&gt;Add more API routes and deploy updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Enjoy coding! 🚀&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Implementing "Forgot Password" with MongoDB: A Step-by-Step Guide</title>
      <dc:creator>pwnkdm</dc:creator>
      <pubDate>Fri, 07 Mar 2025 04:47:30 +0000</pubDate>
      <link>https://dev.to/pwnkdm/implementing-forgot-password-with-mongodb-a-step-by-step-guide-1a0n</link>
      <guid>https://dev.to/pwnkdm/implementing-forgot-password-with-mongodb-a-step-by-step-guide-1a0n</guid>
      <description>&lt;p&gt;A "Forgot Password" feature is essential for any user authentication system. It enhances user experience and security by allowing users to reset their passwords safely. In this guide, we’ll walk through implementing this feature using MongoDB, Node.js, Express, and JWT (JSON Web Token).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we begin, ensure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js &amp;amp; npm installed&lt;/li&gt;
&lt;li&gt;MongoDB set up (local or cloud-based like MongoDB Atlas)&lt;/li&gt;
&lt;li&gt;Basic knowledge of Express &amp;amp; JWT&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Set Up Your Project&lt;/strong&gt;&lt;br&gt;
Initialize a new Node.js project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir forgot-password &amp;amp;&amp;amp; cd forgot-password
npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install required dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express mongoose dotenv jsonwebtoken bcryptjs nodemailer cors body-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Configure MongoDB &amp;amp; Express&lt;/strong&gt;&lt;br&gt;
Create an .env file to store database credentials:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
EMAIL_USER=your_email@example.com
EMAIL_PASS=your_email_password

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set up Express and MongoDB connection in server.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config();

const app = express();
app.use(express.json());

mongoose.connect(process.env.MONGO_URI)
    .then(() =&amp;gt; console.log("MongoDB Connected"))
    .catch(err =&amp;gt; console.error(err));

app.listen(5000, () =&amp;gt; console.log("Server running on port 5000"));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Create the User Model&lt;/strong&gt;&lt;br&gt;
Define a User schema in models/User.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema({
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    resetToken: { type: String, default: null },
    resetTokenExpiry: { type: Date, default: null }
});

module.exports = mongoose.model("User", UserSchema);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Implement the Password Reset Flow&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1️⃣ Generate Reset Token&lt;/strong&gt;&lt;br&gt;
Create an endpoint in routes/auth.js to send a reset email:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const User = require("../models/User");
const jwt = require("jsonwebtoken");
const nodemailer = require("nodemailer");

const router = express.Router();

router.post("/forgot-password", async (req, res) =&amp;gt; {
    const { email } = req.body;
    const user = await User.findOne({ email });

    if (!user) return res.status(404).json({ message: "User not found" });

    const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: "1h" });

    user.resetToken = token;
    user.resetTokenExpiry = Date.now() + 3600000; // 1 hour
    await user.save();

    // Email setup
    const transporter = nodemailer.createTransport({
        service: "gmail",
        auth: {
            user: process.env.EMAIL_USER,
            pass: process.env.EMAIL_PASS
        }
    });

    const mailOptions = {
        to: user.email,
        subject: "Password Reset",
        text: `Click the link to reset your password: http://localhost:3000/reset-password/${token}`
    };

    await transporter.sendMail(mailOptions);

    res.json({ message: "Reset link sent to email" });
});

module.exports = router;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2️⃣ Reset Password&lt;/strong&gt;&lt;br&gt;
Create an endpoint to reset the password:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;router.post("/reset-password", async (req, res) =&amp;gt; {
    const { token, newPassword } = req.body;

    try {
        const decoded = jwt.verify(token, process.env.JWT_SECRET);
        const user = await User.findOne({ _id: decoded.id, resetToken: token });

        if (!user || user.resetTokenExpiry &amp;lt; Date.now()) {
            return res.status(400).json({ message: "Invalid or expired token" });
        }

        user.password = require("bcryptjs").hashSync(newPassword, 10);
        user.resetToken = null;
        user.resetTokenExpiry = null;
        await user.save();

        res.json({ message: "Password reset successful" });
    } catch (err) {
        res.status(400).json({ message: "Invalid token" });
    }
});

module.exports = router;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Test the Implementation&lt;/strong&gt;&lt;br&gt;
Send a POST request to &lt;code&gt;/forgot-password&lt;/code&gt; with &lt;code&gt;{ "email": "user@example.com" }&lt;/code&gt;.&lt;br&gt;
Check your email for the reset link.&lt;br&gt;
Send a POST request to &lt;code&gt;/reset-password&lt;/code&gt; with &lt;code&gt;{ "token": "received_token", "newPassword": "new_secure_password" }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
You’ve  successfully implemented a &lt;strong&gt;secure password reset&lt;/strong&gt; feature with MongoDB, Node.js, and Express. This approach ensures security by &lt;strong&gt;validating tokens, hashing passwords, and setting expiration times&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;🚀 Next Steps? Implement front-end integration and improve security with &lt;strong&gt;rate limiting and better email templates!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>forgetpassword</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
