<?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: Adedapo Toki</title>
    <description>The latest articles on Forem by Adedapo Toki (@matrix24483).</description>
    <link>https://forem.com/matrix24483</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%2F1724379%2F0efdf91c-b17e-443e-9073-5cba3fbd77be.jpg</url>
      <title>Forem: Adedapo Toki</title>
      <link>https://forem.com/matrix24483</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/matrix24483"/>
    <language>en</language>
    <item>
      <title>Setting up a full-stack MERN (MongoDB, Express, React, Node.js) app for deployment on Vercel.</title>
      <dc:creator>Adedapo Toki</dc:creator>
      <pubDate>Sun, 14 Jul 2024 00:17:00 +0000</pubDate>
      <link>https://forem.com/matrix24483/setting-up-a-full-stack-mern-mongodb-express-react-nodejs-app-for-deployment-on-vercel-2mpp</link>
      <guid>https://forem.com/matrix24483/setting-up-a-full-stack-mern-mongodb-express-react-nodejs-app-for-deployment-on-vercel-2mpp</guid>
      <description>&lt;p&gt;The MERN stack, comprising MongoDB, Express, React, and Node.js, is one of several technological combinations used for building full-stack web applications. Vercel provides a versatile platform capable of deploying a wide range of frameworks and technologies for various project types. While often associated with frontend hosting, Vercel also supports backend deployments, making it suitable for MERN applications. &lt;/p&gt;

&lt;p&gt;This article will guide you through the process of setting up a MERN stack application and deploying it on Vercel. We'll cover the steps from local development to deployment, showing how to utilize Vercel's free tier for testing your project and, if desired, hosting it long-term. By the end of this guide, you'll understand how to leverage the MERN stack and Vercel's deployment capabilities to bring your web application online and make it accessible to users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up the Development Environment&lt;/strong&gt;&lt;br&gt;
To beign, you'll need to install the following tools:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Node.js and npm&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit &lt;a href="https://nodejs.org/en/download/package-manager" rel="noopener noreferrer"&gt;https://nodejs.org/en/download/package-manager&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Download and install the LTS (Long Term Support) version&lt;/li&gt;
&lt;li&gt;This installation includes npm (Node Package Manager)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Visual Studio Code (VS Code):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;https://code.visualstudio.com/download&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Download and install the version appropriate for your operating system&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After installation, verify that Node.js and npm are correctly installed by opening a terminal or command prompt and running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node --version
npm --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Microsoft Windows [Version 10.0.22000.3079]
(c) Microsoft Corporation. All rights reserved.

C:\Windows\system32&amp;gt;node --version
v18.14.2

C:\Windows\system32&amp;gt;npm --version
9.5.0

C:\Windows\system32&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Creating a new React app&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your terminal or command prompt&lt;/li&gt;
&lt;li&gt;Navigate to the directory where you want to create your project&lt;/li&gt;
&lt;li&gt;Run the following command:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create-react-app client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Once the installation is complete, navigate into your new project directory:
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Start the development server to ensure everything is working:
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;p&gt;Your default web browser should open and display the default React app page.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up the Node.js/Express server&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In your terminal. navigate back to your main project directory&lt;/li&gt;
&lt;li&gt;Create a new directory for your server:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir server
cd server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initialize a new Node.js project:
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Install the necessary dependencies:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i express bodyParser mongodb mongoose cors dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new file named index.js in the server directory and add the following basic Express server setup:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js
require('dotenv').config();

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());

app.use("/", (req, res) =&amp;gt; {
  res.send("Server running.");
});

const port = process.env.PORT || 9000;

app.listen(port, () =&amp;gt; {
  console.log(`Server is running on port ${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add a start script to your package.json file in the server directory
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "start": "nodemon index.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Start your server:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS C:\Users\Matrix\Documents\D.M.F\server&amp;gt; npm start build

&amp;gt; server@1.0.0 start
&amp;gt; nodemon index.js build

[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node index.js build`
Server is running on port 9000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Configuring the Backend&lt;/strong&gt;&lt;br&gt;
create a new .js file in your backend directory and add the following code:&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 connectDB = async () =&amp;gt; {
  try {
    await mongoose.connect(process.env.MONGODB_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    console.log('MongoDB connected successfully');
  } catch (error) {
    console.error('MongoDB connection error:', error);
    process.exit(1);
  }
};

module.exports = connectDB;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Setting up MongoDB connection and Creating environment variables&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Log in to your MongoDB Atlas account &lt;a href="https://www.mongodb.com/cloud/atlas/register" rel="noopener noreferrer"&gt;https://www.mongodb.com/cloud/atlas/register&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Navigate to your cluster's Network Access settings&lt;/li&gt;
&lt;li&gt;Click on "Add IP Address",&lt;/li&gt;
&lt;li&gt;To allow connections from any IP address (suitable for development but should be restricted for production), enter:
IP Address: 0.0.0.0/0
Description: Allow access from anywhere&lt;/li&gt;
&lt;li&gt;Click "Confirm".&lt;/li&gt;
&lt;/ol&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%2Fs3alpo7jqm1k92b9ljs1.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%2Fs3alpo7jqm1k92b9ljs1.png" alt="Image description" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Important:&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Allowing access from 0.0.0.0/0 means your database can be accessed from any IP address. This is convenient for development and testing, but for a production environment, you should restrict access to only the necessary IP addresses or ranges for security reasons.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Create a .env file in your backend directory and add your MongoDB connection string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGODB_URI=your_mongodb_connection_string_here
PORT=9000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;your_mongodb_connection_string_here&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Structuring the Express app&lt;/strong&gt;&lt;br&gt;
Now, update &lt;code&gt;index.js&lt;/code&gt; file to incorporate these changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const connectDB = require('./db');

const app = express();

// Connect to MongoDB
connectDB();

// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json());

// Routes
app.get("/", (req, res) =&amp;gt; {
  res.send("Server deployed and running on vercel.");
});

// You can add more route files here as your application grows
// app.use('/api/users', require('./routes/users'));
// app.use('/api/posts', require('./routes/posts'));

const port = process.env.PORT || 9000;

app.listen(port, () =&amp;gt; {
  console.log(`Server is running on port ${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Developing the Frontend&lt;/strong&gt;&lt;br&gt;
For the purpose of this article, we'll be using the following structure to illustrate how to organize a React frontend. Keep in mind that React is highly flexible, and you can adapt this organization to best suit your project's specific requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React App Structure&lt;/strong&gt;&lt;br&gt;
Let's consider the following structure for our React application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; client/
   ├── src/
   │   ├── components/
   │   ├── services/
   │   ├── styles/
   │   ├── App.js
   │   └── index.js
   ├── package.json
   └── README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;components/: Contains all React components&lt;/li&gt;
&lt;li&gt; services/: Houses API-related code&lt;/li&gt;
&lt;li&gt; styles/: Stores CSS files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Creating Necessary Components&lt;/strong&gt;&lt;br&gt;
For this project, I've created several key components. Here's an example of one of the main components, &lt;code&gt;UserManagement.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from 'react';
import api from '../services/api';
import IncomingRequestTable from './IncomingRequestTable';
import MembersTable from './MembersTable';
import VolunteerTable from './VolunteerTable';
import './user-management.css';

const UserManagement = () =&amp;gt; {
  const [showIncomingRequests, setShowIncomingRequests] = useState(true);
  const [incomingRequests, setIncomingRequests] = useState([]);
  const [teamMembers, setTeamMembers] = useState([]);
  const [showVolunteers, setShowVolunteers] = useState(false);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() =&amp;gt; {
    fetchIncomingRequests();
    fetchTeamMembers();
  }, []);

  // Fetch functions and other logic here...

  return (
    &amp;lt;div className="user-management-main-container"&amp;gt;
      {/* Component JSX here... */}
    &amp;lt;/div&amp;gt;
  );
};

export default UserManagement;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This component manages the display of different user tables and handles data fetching.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Implementing API Calls to the Backend&lt;/strong&gt;&lt;br&gt;
To interact with our backend, I've created an &lt;code&gt;api.js&lt;/code&gt; file in the services folder. Here's a snippet of how it's structured:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const BASE_URL = 'https://localhost:9000';

const handleResponse = async (response) =&amp;gt; {
  if (!response.ok) {
    const error = await response.text();
    throw new Error(error);
  }
  return response.json();
};

const api = {
  team: {
    getMembers: (status) =&amp;gt; 
      fetch(`${BASE_URL}/get-team-members?status=${status}`)
        .then(handleResponse),

    acceptRequest: (userId) =&amp;gt;
      fetch(`${BASE_URL}/accept-request/${userId}`, {
        method: 'POST',
      }).then(handleResponse),

    // Other API methods...
  },
  // Other API categories...
};

export default api;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This centralized API structure allows for easy management of all backend requests.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the components, we use these API calls like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fetchIncomingRequests = async () =&amp;gt; {
  try {
    const data = await api.team.getMembers('pending');
    setIncomingRequests(data);
  } catch (error) {
    setError('Error fetching incoming requests');
  } finally {
    setLoading(false);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach keeps our components clean and our API calls organized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Connecting Frontend and Backend&lt;/strong&gt;&lt;br&gt;
In this section, we'll focus on configuring CORS (Cross-Origin Resource Sharing) and testing local communication between our React frontend and Express backend.&lt;/p&gt;

&lt;p&gt;Configuring CORS&lt;br&gt;
CORS is a crucial security feature that needs to be properly configured to allow our frontend to communicate with the backend. In&lt;br&gt;&lt;br&gt;
 &lt;code&gt;index.js&lt;/code&gt; file, we've already set up CORS:&lt;br&gt;
&lt;/p&gt;

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

app.use(cors());

 app.use(cors({
   origin: 'http://localhost:9000'  //to be changed later to vercel url
 }));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Testing Local Communication&lt;/strong&gt;&lt;br&gt;
To test the communication between your frontend and backend locally:&lt;/p&gt;

&lt;p&gt;Start your backend server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see a message: &lt;code&gt;Server is running on port 9000&lt;/code&gt;&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%2Ffm6z91sfndh2f6hoccvd.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%2Ffm6z91sfndh2f6hoccvd.png" alt="Image description" width="800" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your frontend's api.js file, ensure you're using the correct local URL. For this article:&lt;br&gt;
&lt;code&gt;const BASE_URL = 'http://localhost:9000';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Start your React development server:&lt;br&gt;
&lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Test an API call, for example, fetching team members:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;javascriptCopyconst fetchTeamMembers = async () =&amp;gt; {
  try {
    const response = await fetch(`${BASE_URL}/get-team-members?status=accepted`);
    const data = await response.json();
    console.log('Team members:', data);
  } catch (error) {
    console.error('Error fetching team members:', error);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check your browser's console and network tab to ensure the request is successful and data is being received.&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%2Fbvqffahg1xsw6x7lsoge.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%2Fbvqffahg1xsw6x7lsoge.png" alt="Image description" width="800" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you encounter any CORS errors, double-check your CORS configuration in the backend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preparing for Deployment&lt;/strong&gt;&lt;br&gt;
In this section, we'll focus on creating a Vercel configuration file and pushing your code to GitHub repositories, which are crucial steps for deploying your application on Vercel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Vercel Configuration File&lt;/strong&gt;&lt;br&gt;
Vercel uses a configuration file named &lt;code&gt;vercel.json&lt;/code&gt; to specify how to build and deploy your application. Here's the configuration file we'll use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "version": 2,
    "builds": [
      {
        "src": "*.js",
        "use": "@vercel/node"
      }
    ],
    "routes": [
      {
         "src": "/(.*)",
         "dest": "/",
          "methods": ["GET","POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
          "headers": {
             "Access-Control-Allow-Origin": "*",
             "Access-Control-Allow-Credentials": "true",
             "Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
         }
     }
   ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration does the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specifies that we're using version 2 of Vercel's configuration.&lt;/li&gt;
&lt;li&gt;Sets up builds for all JavaScript files using the Node.js runtime.&lt;/li&gt;
&lt;li&gt;Configures routes to handle all HTTP methods and sets up CORS headers.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Create this file in the root of your backend project and name it &lt;code&gt;vercel.json&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Pushing Code to GitHub Repositories&lt;/strong&gt;&lt;br&gt;
Before deploying, you need to push your code to GitHub:&lt;br&gt;
For this project,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;create two repositories on GitHub: one for your frontend and one for your backend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In your backend directory, initialize a Git repository if you haven't already:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Add your files and commit:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "Initial commit for backend"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Add your GitHub repository as a remote and push:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add origin https://github.com/your-username/your-backend-repo.git
git branch -M main
git push -u origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Repeat steps 2-4 for your frontend directory, using the frontend GitHub repository URL.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Installing and Using Vercel CLI&lt;/strong&gt;&lt;br&gt;
To deploy your application using Vercel, you'll need to install and use the Vercel CLI:&lt;/p&gt;

&lt;p&gt;Install Vercel CLI globally:&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 -g vercel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify the installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`vercel --version`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Log in to your Vercel account:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&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%2Fcwg7qu0kxsxqdo4c860t.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%2Fcwg7qu0kxsxqdo4c860t.png" alt="Vercel Login CLI" width="800" height="312"&gt;&lt;/a&gt;&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%2F83kgnj5gek9jxvhxfz4y.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%2F83kgnj5gek9jxvhxfz4y.png" alt="Vercel Login Success" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deploying to Vercel&lt;/strong&gt;&lt;br&gt;
Before starting, ensure you're logged into your Vercel account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deploying the React Frontend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the Vercel dashboard, click on &lt;code&gt;"Add New"&lt;/code&gt; and select &lt;code&gt;"Project"&lt;/code&gt; from the dropdown.&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%2Frtw2i777oc1p6ot4dmbn.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%2Frtw2i777oc1p6ot4dmbn.png" alt="Vercel Dashboard" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Choose &lt;code&gt;"Import Git Repository"&lt;/code&gt; and select your frontend repository.&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%2Fc2iv0m9efeltiyo188iu.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%2Fc2iv0m9efeltiyo188iu.png" alt="Selecting Repo" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Configure the project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enter a project name&lt;/li&gt;
&lt;li&gt;For Framework Preset, select &lt;code&gt;"Create React App"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set Root Directory to "./" (if your package.json is in the root)&lt;/li&gt;
&lt;li&gt;Leave Build and Output Settings as default&lt;/li&gt;
&lt;li&gt;Leave Environment Variables as default&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%2Fsdnukm58mn9aqjfcunyb.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%2Fsdnukm58mn9aqjfcunyb.png" alt="Configurations" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;code&gt;"Deploy"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Vercel will now build and deploy your React frontend. Once complete, you'll receive a URL for your deployed frontend.&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%2F0cqbopsbf69n7pcat8j9.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%2F0cqbopsbf69n7pcat8j9.png" alt="Image description" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deploying the Node.js Backend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Again, click on &lt;code&gt;"Add New"&lt;/code&gt; and select &lt;code&gt;"Project"&lt;/code&gt;.&lt;br&gt;
Choose &lt;code&gt;"Import Git Repository"&lt;/code&gt; and select your backend repository.&lt;/p&gt;

&lt;p&gt;Configure the project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enter a project name&lt;/li&gt;
&lt;li&gt;For Framework Preset, select "Other"&lt;/li&gt;
&lt;li&gt;Set Root Directory to "./" (if your package.json is in the root)&lt;/li&gt;
&lt;li&gt;Leave Build and Output Settings as default&lt;/li&gt;
&lt;li&gt;Configuring &lt;code&gt;"Environment Variables"&lt;/code&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add your environment variables exactly as they appear in your local .env file. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Key: &lt;code&gt;MONGODB_URI&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Value: &lt;code&gt;mongodb+srv://your_username:your_password@your_cluster.mongodb.net/your_database?retryWrites=true&amp;amp;w=majority&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add any other necessary environment variables (e.g., PORT, JWT_SECRET, etc.)&lt;/p&gt;

&lt;p&gt;Click &lt;code&gt;"Deploy"&lt;/code&gt;&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%2Fq1ifr2omrttkjh0fje1y.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%2Fq1ifr2omrttkjh0fje1y.png" alt="Environment Variables setup" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Vercel will begin building your backend code. However, the build may take some time due to environment variables.&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%2Fh9ae6xbfp46647m3eq7t.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%2Fh9ae6xbfp46647m3eq7t.png" alt="Success for backend deploy" width="800" height="449"&gt;&lt;/a&gt;&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%2Fdkxgln0tyug39j6isr09.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%2Fdkxgln0tyug39j6isr09.png" alt="Both front and backend deployments" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You've successfully deployed frontend React App and Backend Node js Server to Vercel!!!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Notes:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make sure your backend code is using environment variables correctly, e.g., &lt;code&gt;process.env.MONGODB_URI&lt;/code&gt;.&lt;br&gt;
In your frontend code, update the API base URL to use the deployed backend URL.&lt;br&gt;
If you make changes to your code, push them to GitHub. Vercel will automatically redeploy your application.&lt;br&gt;
Always keep your environment variables secret and never commit them to your repository.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Updating API Endpoints in the Frontend&lt;/strong&gt;&lt;br&gt;
After successfully deploying both your frontend and backend, you need to update the API endpoints in your frontend code to point to the deployed backend URL.&lt;/p&gt;

&lt;p&gt;In your frontend code, locate your API configuration file (e.g.,&lt;code&gt;src/services/api.js&lt;/code&gt;).&lt;br&gt;
Update the BASE_URL to your deployed backend URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const BASE_URL = 'http://localhost:9000'; // Local development
const BASE_URL = 'https://dmfc-server.vercel.app'; // Deployed backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Redeploy your frontend application to Vercel with these changes by pushing to Github.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Testing and Verification&lt;/strong&gt;&lt;br&gt;
Now it's time to thoroughly test your deployed application:&lt;/p&gt;

&lt;p&gt;Open your deployed frontend URL in a web browser.&lt;/p&gt;

&lt;p&gt;Test all functionalities of your application, ensuring they work as expected with the live backend.&lt;/p&gt;

&lt;p&gt;Test your application on different devices and browsers to ensure compatibility.&lt;/p&gt;

&lt;p&gt;Monitor your Vercel logs and MongoDB Atlas dashboard for any errors or unexpected behavior.&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%2Fe6w5m66jcsl7kxuj88ax.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%2Fe6w5m66jcsl7kxuj88ax.png" alt="Frontend" width="800" height="421"&gt;&lt;/a&gt;&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%2Fls9h2xw0egtoucrbcg6k.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%2Fls9h2xw0egtoucrbcg6k.png" alt="Backend" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Congratulations! You've successfully deployed your MERN stack application to Vercel. Here's a summary of what we've accomplished:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a React frontend and Node.js/Express backend with MongoDB integration.&lt;/li&gt;
&lt;li&gt;Prepared our application for deployment by creating necessary configuration files.&lt;/li&gt;
&lt;li&gt;Deployed both frontend and backend to Vercel.&lt;/li&gt;
&lt;li&gt;Configured environment variables to ensure secure and proper functionality.&lt;/li&gt;
&lt;li&gt;Updated API endpoints and performed final testing.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Your application is now live and accessible to users worldwide&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>serverless</category>
      <category>node</category>
      <category>react</category>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
