How Proxy Backend Server using React.js ?
Last Updated :
28 Apr, 2025
When building a web application with React.js, you may need to request a backend server. However, if the backend server does not allow Cross-Origin Resource Sharing (CORS), you may encounter an error when requesting from the frontend application. In this case, you can use the proxy server to forward requests from the front-end application to the back-end server. This can be done in React using a package called "HTTP-proxy-middleware".
Prerequisites:
Follow this step-by-step guide to using a Proxy Backend Server using React JS
Steps to Create ReactJS Application and Installing Modules:
Step 1: Create a new React.js application using the following command
npx create-react-app my-app
Step 2: Install the "HTTP-proxy-middleware" package using the following command
npm i http-proxy-middleware --save
Step 3: Create a new file named "setupProxy.js" in the "src" directory of the React.js application. This information will be used to configure the name server.
Step 4: Add the following code to the "setupProxy.js" file
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:5000',
changeOrigin: true,
})
);
};
In this code, we set the proxy server to send requests to the "/api" path to the backend server running at http://localhost:5000. The "changeOrigin" option set to true allows the backend server to receive requests with the original host header.
Step 5: Update your React.js app to use the server name. For example, if you want to use the "fetch" API to make a request to the backend server, you can use the following code
fetch('/api/data')
.then(response => response.json())
.then(data => console.log(data));
In this code, we request the path "/api/data" which will be sent from the proxy server to the backend server. The response from the backend server is then converted to JSON and saved to the console.
Project Structure:Â
React Js Project StructureExample: This example demonstrates how to use the proxy server in a React.js application.
JavaScript
// Filename - App.js
import React, { useEffect, useState } from 'react';
function App() {
const [data, setData] = useState(null);
useEffect(() => {
fetch('/api/data')
.then(response => response.json())
.then(data => setData(data));
}, []);
return (
<div>
{data ? (
<ul>
{data.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
) : (
<p>Loading...</p>
)}
</div>
);
}
export default App;
JavaScript
// Filename - setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:5000',
changeOrigin: true,
})
);
};
In this code, we use the hooks "useState" and "useEffect" to make a request to the backend server and update the content of the state with the response. Then we present the information in the list. When the component is loaded for the first time, the "Loading... " message will appear until the data is returned from the backend server.
Similar Reads
How to Build a Node.js Proxy Server ? A proxy server acts as an intermediary between a client and other servers. It can be used for various purposes, including load balancing, security, and caching. In the context of web development, a proxy server forwards requests from clients to other servers, fetches responses, and sends them back t
4 min read
How to use TypeScript on backend ? TypeScript, developed by Microsoft, enhances JavaScript by adding type safety, which significantly simplifies debugging and development. This guide will walk you through setting up a TypeScript backend with Node.js and Express, showing you how to leverage TypeScriptâs robust features to prevent comm
2 min read
How To Start Next.js Server? Next.js is a React framework created by Vercel that helps developers build server-side rendered and static web applications. Starting a Next.js server is a simple process that allows you to see your application running in a local development environment or a production environment. PrerequisitesNode
2 min read
How to Prevent Access to Admin Pages using Node.js and React.js ? To prevent access to admin pages using Node.js and React.js is a common task to avoid unauthorized access. In many websites, the content available to the end-user is limited until he/she proves his/her authenticity by logging in. During the development of MERN Stack Applications, when the server is
6 min read
How React and ReactDOM works? When you work with React, it is more than likely that you will build your apps with JSX. The JSX is a tag-based JavaScript syntax like looks familiar with HTML. React element is the atomic and most basic unit that you need to master before JSX and before moving forward with React. Note: In order to
9 min read
Node.js Server Side Rendering (SSR) using EJS Server-side rendering (SSR) is a popular technique for rendering a normally client-side-only single-page app (SPA) on the server and then sending a fully rendered page to the client. The client's JavaScript bundle can then take over and the SPA can operate as normal. SSR technique is helpful in situ
3 min read
ReactJS Server Components React is a JavaScript library which is used to develop frontend of websites. Since it's introduction in the year 2013 React has gained a lot of popularity among the web developers around the world. The library has a great support from the community and is evolving at a high speed from its launch. In
3 min read
React JS Basic Concepts Reference React is a library and not a framework. It was developed by Facebook to address its specific challenges. Understanding a new library or framework can be exciting, but it's important to first grasp its core concepts. Setting Up the Development EnvironmentBefore going into the practical aspects of Rea
4 min read
How to Deploy Node Backend on Vercel ? To deploy a Node.js backend to Vercel, you can follow the simple approach that involves setting up your Node.js project, configuring necessary files, and using the Vercel CLI. Vercel is known for its ease of use and automatic optimizations, making it an excellent choice for deploying full-stack appl
2 min read
How to Use Node.js for Backend Web Development? In the world of website design nowadays, NodeJS is a supportive tool for making capable backend systems. Whether you are an experienced web designer or just beginning out, NodeJS can upgrade your aptitude and help you in building extraordinary websites. This guide will show you how to use NodeJS to
8 min read