<?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: R Deepti</title>
    <description>The latest articles on DEV Community by R Deepti (@r_deepti_6ef855c6b6cca0dc).</description>
    <link>https://dev.to/r_deepti_6ef855c6b6cca0dc</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%2F3569617%2Fdb0a8cb6-91d9-497a-9c35-97a2b564d0e0.jpg</url>
      <title>DEV Community: R Deepti</title>
      <link>https://dev.to/r_deepti_6ef855c6b6cca0dc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/r_deepti_6ef855c6b6cca0dc"/>
    <language>en</language>
    <item>
      <title>Beyond localhost:3000: How to Finally Connect Your Frontend and Backend</title>
      <dc:creator>R Deepti</dc:creator>
      <pubDate>Thu, 16 Oct 2025 19:10:27 +0000</pubDate>
      <link>https://dev.to/r_deepti_6ef855c6b6cca0dc/beyond-localhost3000-how-to-finally-connect-your-frontend-and-backend-5hka</link>
      <guid>https://dev.to/r_deepti_6ef855c6b6cca0dc/beyond-localhost3000-how-to-finally-connect-your-frontend-and-backend-5hka</guid>
      <description>&lt;p&gt;If you're learning full-stack development, you've probably been here before:&lt;/p&gt;

&lt;p&gt;You've built a beautiful React frontend. You've created a powerful Express.js backend. You start both servers - frontend on localhost:3000, backend on localhost:5000 - and then... CORS error.&lt;/p&gt;

&lt;p&gt;That dreaded red message in the console:&lt;/p&gt;

&lt;p&gt;text&lt;br&gt;
Access to fetch at '&lt;a href="http://localhost:5000/api/data" rel="noopener noreferrer"&gt;http://localhost:5000/api/data&lt;/a&gt;' from origin '&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;' has been blocked by CORS policy&lt;br&gt;
Don't worry! This is a rite of passage for every full-stack developer. In this guide, I'll show you three clean ways to connect your frontend and backend, from quick fixes to production-ready solutions.&lt;/p&gt;

&lt;p&gt;Understanding the Problem: Why CORS Exists&lt;br&gt;
First, let's understand why this happens. Browsers have a security feature called CORS (Cross-Origin Resource Sharing) that blocks requests between different origins by default.&lt;/p&gt;

&lt;p&gt;An "origin" includes the protocol, domain, and port. So:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; = one origin&lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt; = different origin&lt;/p&gt;

&lt;p&gt;The browser blocks your React app from talking to your Express server because they're on different ports. It's a security feature, but in development, it's our biggest headache.&lt;/p&gt;

&lt;p&gt;What We're Building&lt;br&gt;
Let's set up a simple example. We have:&lt;/p&gt;

&lt;p&gt;Backend (server.js) - Express API on port 5000:&lt;/p&gt;

&lt;p&gt;javascript&lt;br&gt;
const express = require('express');&lt;br&gt;
const app = express();&lt;br&gt;
const PORT = 5000;&lt;/p&gt;

&lt;p&gt;app.get('/api/books', (req, res) =&amp;gt; {&lt;br&gt;
  res.json([&lt;br&gt;
    { id: 1, title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' },&lt;br&gt;
    { id: 2, title: 'Dune', author: 'Frank Herbert' }&lt;br&gt;
  ]);&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;app.listen(PORT, () =&amp;gt; {&lt;br&gt;
  console.log(&lt;code&gt;Backend running on http://localhost:${PORT}&lt;/code&gt;);&lt;br&gt;
});&lt;br&gt;
Frontend (App.js) - React app on port 3000 that needs to display the books.&lt;/p&gt;

&lt;p&gt;Solution 1: The Proxy Method (Easiest for Development)&lt;br&gt;
If you're using Create React App or Vite, this is the simplest solution.&lt;/p&gt;

&lt;p&gt;Step 1: Configure the Proxy&lt;br&gt;
In your frontend's package.json, add this line:&lt;/p&gt;

&lt;p&gt;json&lt;br&gt;
{&lt;br&gt;
  "name": "my-react-app",&lt;br&gt;
  "version": "0.1.0",&lt;br&gt;
  "proxy": "&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;",&lt;br&gt;
  "dependencies": {&lt;br&gt;
    // your dependencies&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
Step 2: Update Your API Calls&lt;br&gt;
Change your React component to use relative paths instead of the full URL:&lt;/p&gt;

&lt;p&gt;javascript&lt;br&gt;
// App.js&lt;br&gt;
import React, { useState, useEffect } from 'react';&lt;/p&gt;

&lt;p&gt;function App() {&lt;br&gt;
  const [books, setBooks] = useState([]);&lt;br&gt;
  const [loading, setLoading] = useState(true);&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
    // Notice: we're using a relative path now&lt;br&gt;
    fetch('/api/books')&lt;br&gt;
      .then(response =&amp;gt; response.json())&lt;br&gt;
      .then(data =&amp;gt; {&lt;br&gt;
        setBooks(data);&lt;br&gt;
        setLoading(false);&lt;br&gt;
      })&lt;br&gt;
      .catch(error =&amp;gt; {&lt;br&gt;
        console.error('Error fetching books:', error);&lt;br&gt;
        setLoading(false);&lt;br&gt;
      });&lt;br&gt;
  }, []);&lt;/p&gt;

&lt;p&gt;if (loading) return &lt;/p&gt;Loading books...;

&lt;p&gt;return (&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;
      &lt;h1&gt;My Book Collection&lt;/h1&gt;
&lt;br&gt;
      &lt;ul&gt;

        {books.map(book =&amp;gt; (
          &lt;li&gt;

            &lt;strong&gt;{book.title}&lt;/strong&gt; by {book.author}
          &lt;/li&gt;

        ))}
      &lt;/ul&gt;
&lt;br&gt;
    &lt;br&gt;
  );&lt;br&gt;
}

&lt;p&gt;export default App;&lt;br&gt;
How This Works&lt;br&gt;
When your React app makes a request to /api/books, the development server recognizes it's not a static asset and forwards it to &lt;a href="http://localhost:5000/api/books" rel="noopener noreferrer"&gt;http://localhost:5000/api/books&lt;/a&gt;. The browser only sees one origin (localhost:3000), so no CORS error!&lt;/p&gt;

&lt;p&gt;Restart your frontend server after adding the proxy configuration.&lt;/p&gt;

&lt;p&gt;Solution 2: Enable CORS on the Backend (More Flexible)&lt;br&gt;
Sometimes you need more control, or you're not using Create React App. In this case, configure CORS on your Express server.&lt;/p&gt;

&lt;p&gt;Step 1: Install the CORS package&lt;br&gt;
bash&lt;br&gt;
cd backend&lt;br&gt;
npm install cors&lt;br&gt;
Step 2: Update Your Server&lt;br&gt;
javascript&lt;br&gt;
const express = require('express');&lt;br&gt;
const cors = require('cors');  // Import the package&lt;br&gt;
const app = express();&lt;br&gt;
const PORT = 5000;&lt;/p&gt;

&lt;p&gt;// Enable CORS for all routes&lt;br&gt;
app.use(cors());&lt;/p&gt;

&lt;p&gt;// Or be more specific - only allow your frontend&lt;br&gt;
// app.use(cors({&lt;br&gt;
//   origin: '&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;'&lt;br&gt;
// }));&lt;/p&gt;

&lt;p&gt;app.get('/api/books', (req, res) =&amp;gt; {&lt;br&gt;
  res.json([&lt;br&gt;
    { id: 1, title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' },&lt;br&gt;
    { id: 2, title: 'Dune', author: 'Frank Herbert' }&lt;br&gt;
  ]);&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;app.listen(PORT, () =&amp;gt; {&lt;br&gt;
  console.log(&lt;code&gt;Backend running on http://localhost:${PORT}&lt;/code&gt;);&lt;br&gt;
});&lt;br&gt;
Step 3: Update Your Frontend (Use Full URL)&lt;br&gt;
Now you can use the full URL in your fetch calls:&lt;/p&gt;

&lt;p&gt;javascript&lt;br&gt;
// In your React component&lt;br&gt;
fetch('&lt;a href="http://localhost:5000/api/books'" rel="noopener noreferrer"&gt;http://localhost:5000/api/books'&lt;/a&gt;)&lt;br&gt;
  .then(response =&amp;gt; response.json())&lt;br&gt;
  .then(data =&amp;gt; setBooks(data));&lt;br&gt;
Solution 3: Environment Variables (Production Ready)&lt;br&gt;
Hardcoding URLs is bad practice. Let's make this production-ready with environment variables.&lt;/p&gt;

&lt;p&gt;Frontend Configuration&lt;br&gt;
Create a .env file in your frontend root:&lt;/p&gt;

&lt;p&gt;env&lt;/p&gt;

&lt;h1&gt;
  
  
  .env in your frontend
&lt;/h1&gt;

&lt;p&gt;REACT_APP_API_URL=&lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;&lt;br&gt;
Important: Variable names must start with REACT_APP_ for Create React App to recognize them.&lt;/p&gt;

&lt;p&gt;Update your React component:&lt;/p&gt;

&lt;p&gt;javascript&lt;br&gt;
// App.js&lt;br&gt;
const API_URL = process.env.REACT_APP_API_URL;&lt;/p&gt;

&lt;p&gt;useEffect(() =&amp;gt; {&lt;br&gt;
  fetch(&lt;code&gt;${API_URL}/api/books&lt;/code&gt;)&lt;br&gt;
    .then(response =&amp;gt; response.json())&lt;br&gt;
    .then(data =&amp;gt; {&lt;br&gt;
      setBooks(data);&lt;br&gt;
      setLoading(false);&lt;br&gt;
    });&lt;br&gt;
}, []);&lt;br&gt;
Backend Configuration&lt;br&gt;
Create a .env file in your backend:&lt;/p&gt;

&lt;p&gt;env&lt;/p&gt;

&lt;h1&gt;
  
  
  .env in your backend
&lt;/h1&gt;

&lt;p&gt;PORT=5000&lt;br&gt;
CLIENT_URL=&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;br&gt;
Update your server to use these variables:&lt;/p&gt;

&lt;p&gt;javascript&lt;br&gt;
require('dotenv').config();  // Install with: npm install dotenv&lt;/p&gt;

&lt;p&gt;const express = require('express');&lt;br&gt;
const cors = require('cors');&lt;br&gt;
const app = express();&lt;br&gt;
const PORT = process.env.PORT || 5000;&lt;/p&gt;

&lt;p&gt;app.use(cors({&lt;br&gt;
  origin: process.env.CLIENT_URL&lt;br&gt;
}));&lt;/p&gt;

&lt;p&gt;// ... rest of your server code&lt;br&gt;
Putting It All Together: Complete Workflow&lt;br&gt;
Here's my recommended setup for different scenarios:&lt;/p&gt;

&lt;p&gt;Development Setup&lt;br&gt;
Use Proxy method for Create React App&lt;/p&gt;

&lt;p&gt;Use CORS package for other frontend frameworks&lt;/p&gt;

&lt;p&gt;Always use environment variables from day one&lt;/p&gt;

&lt;p&gt;Production Setup&lt;br&gt;
Deploy your backend (Heroku, Railway, DigitalOcean)&lt;/p&gt;

&lt;p&gt;Deploy your frontend (Netlify, Vercel)&lt;/p&gt;

&lt;p&gt;Update your environment variables:&lt;/p&gt;

&lt;p&gt;Frontend: REACT_APP_API_URL=&lt;a href="https://your-backend.herokuapp.com" rel="noopener noreferrer"&gt;https://your-backend.herokuapp.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Backend: CLIENT_URL=&lt;a href="https://your-frontend.netlify.app" rel="noopener noreferrer"&gt;https://your-frontend.netlify.app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Testing Your Connection&lt;br&gt;
Create a simple test to verify everything works:&lt;/p&gt;

&lt;p&gt;javascript&lt;br&gt;
// Add this to your backend for testing&lt;br&gt;
app.get('/api/health', (req, res) =&amp;gt; {&lt;br&gt;
  res.json({ status: 'OK', message: 'Backend is running!' });&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;// Test from your frontend&lt;br&gt;
fetch('/api/health')&lt;br&gt;
  .then(response =&amp;gt; response.json())&lt;br&gt;
  .then(data =&amp;gt; console.log('Connection test:', data));&lt;/p&gt;

&lt;p&gt;Troubleshooting Common Issues&lt;br&gt;
❌ "Network Error" or CORS block&lt;br&gt;
Fix: Check your proxy setting or CORS configuration&lt;/p&gt;

&lt;p&gt;Verify: Backend is running on the correct port&lt;/p&gt;

&lt;p&gt;❌ Environment variables not working&lt;br&gt;
Fix: Restart your development server after adding .env files&lt;/p&gt;

&lt;p&gt;Verify: Variable names start with REACT_APP_&lt;/p&gt;

&lt;p&gt;❌ Proxy not working&lt;br&gt;
Fix: Ensure you're using relative paths (/api/books) not full URLs&lt;/p&gt;

&lt;p&gt;Verify: You restarted the frontend after adding the proxy&lt;/p&gt;

&lt;p&gt;Next Steps&lt;br&gt;
Now that you've connected your frontend and backend, consider:&lt;/p&gt;

&lt;p&gt;Adding error handling to your API calls&lt;/p&gt;

&lt;p&gt;Implementing authentication&lt;/p&gt;

&lt;p&gt;Setting up a production database&lt;/p&gt;

&lt;p&gt;Adding API rate limiting&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
Connecting frontend and backend doesn't have to be scary. The key takeaways:&lt;/p&gt;

&lt;p&gt;Use proxies for simple Create React App setups&lt;/p&gt;

&lt;p&gt;Configure CORS when you need more control&lt;/p&gt;

&lt;p&gt;Always use environment variables for API URLs&lt;/p&gt;

&lt;p&gt;Test your connection before building more features&lt;/p&gt;

&lt;p&gt;You've now conquered one of the biggest hurdles in full-stack development. Go build something amazing!&lt;/p&gt;

&lt;p&gt;Enjoyed this article? Found another solution that worked for you? Share your experiences and questions in the comments below! Let's learn together. 👇&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
