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