SoFunction
Updated on 2025-03-10

5 ways to configure proxy server and usage scenarios for React

Introduction to five methods

Here are the usage scenarios and advantages and disadvantages of five methods of configuring proxy servers in React projects:

1. Usehttp-proxy-middleware middleware:

  • Usage scenario: Suitable for most React projects, simple and easy to use.
  • Advantages: Simple configuration, easy to understand and maintain.
  • Disadvantages: Need to be created manually files and additional middleware needs to be installed.

2. Usehttp-proxy-middleware of document:

  • Usage scenario: Suitable for most React projects, simple and easy to use.
  • Advantages: Simple configuration, easy to understand and maintain.
  • Disadvantages: Need to be created manually files and additional middleware needs to be installed.

3. Usehttp-proxy-middleware Configuration file:

  • Usage scenario: Applicable to older React projects, or projects that require older syntax of middleware.
  • Advantages: Simple configuration, easy to understand and maintain.
  • Disadvantages: The configuration file needs to be created manually, and additional middleware needs to be installed.

4. Usehttp-proxy-middleware of Configuration:

  • Usage scenario: Suitable for projects that do not require custom configuration.
  • Advantages: Simple configuration, no additional files and middleware required.
  • Disadvantages: Limited functionality and not suitable for complex proxy configurations.

5. Use Files and custom configurations:

  • Usage scenario: Applicable to projects that require more complex proxy configurations, such as modifying request headers, adding authentication information, etc.
  • Advantages: Flexible configuration, custom configuration can be made according to your needs.
  • Disadvantages: Need to be created manually files and additional middleware needs to be installed.

To sum up, choosing the right method depends on the project’s needs and personal preferences. For most React projects, usehttp-proxy-middleware Middleware or Files are simple and commonly used methods. If you need more complex proxy configurations, you can choose to use Files and custom configurations. and Configuration is suitable for projects that do not require custom configurations.

Code example

There are many ways to configure a proxy server in a React project. Here are several commonly used methods:

1. Usehttp-proxy-middleware middleware:

http-proxy-middleware It is a commonly used proxy middleware that can be used in React projects. First, installhttp-proxy-middleware

npm install http-proxy-middleware --save

Then, in the projectsrc Create a directory Files, and configure proxy servers in it:

const { createProxyMiddleware } = require('http-proxy-middleware');
 = function(app) {
  (
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:5000', // Address of the proxy server      changeOrigin: true,
    })
  );
};

In this way, all/api The beginning requests will be proxied tohttp://localhost:5000

2. Usehttp-proxy-middleware of document:

Create a React project root directorysrc/ Files, and configure proxy servers in it:

const { createProxyMiddleware } = require('http-proxy-middleware');
 = function(app) {
  (
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:5000', // Address of the proxy server      changeOrigin: true,
    })
  );
};

This method is similar to the first method, the difference is that The location and naming of the file.

3. Usehttp-proxy-middleware Configuration file:

Create a React project root directorysrc/ Files, and configure proxy servers in it:

const proxy = require('http-proxy-middleware');
 = function(app) {
  (
    '/api',
    proxy({
      target: 'http://localhost:5000', // Address of the proxy server      changeOrigin: true,
    })
  );
};

This method is similar to the first two methods, the difference is that it useshttp-proxy-middleware old syntax.

This method can be configured more flexiblely as needed, such as modifying the request header, rewriting the request path, etc.

Here are two other methods of configuring proxy servers in React projects:

4. Usehttp-proxy-middleware of Configuration:

In the root directory of the React project In the file, you can add one"proxy" Fields to configure the proxy server. For example:

{
  "name": "my-react-app",
  "version": "1.0.0",
  "proxy": "http://localhost:5000"
}

In this way, all sent to/api Requests will be proxied tohttp://localhost:5000

5. Use Files and custom configurations:

Sometimes, we may need more complex proxy configurations, such as modifying the request header, adding authentication information, etc. At this time, you can Customize the configuration in the file. For example:

const { createProxyMiddleware } = require('http-proxy-middleware');
 = function(app) {
  (
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:5000', // Address of the proxy server      changeOrigin: true,
      headers: {
        Authorization: 'Bearer token123', // Add authentication information      },
      pathRewrite: {
        '^/api': '', // Rewrite the request path and remove the '/api' prefix      },
    })
  );
};

The above are several common methods for configuring proxy servers in React projects. You can choose the appropriate method to configure according to your project needs. No matter which method is used, you need to make sure the address and port of the proxy server are correct, and restart the React development server after the configuration is complete to make the configuration take effect.

This is the article about 5 methods and usage scenarios for React to configure proxy servers. For more related React proxy server content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!