You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在本地运行生产版React应用并对接本地后端(无需修改代码)

How to Run a Production-Built React App Locally with a Local Backend (No Code Changes Needed)

Hey there! Great question—running your production-ready React app locally alongside your backend without modifying any React code is totally achievable. Here’s a step-by-step breakdown of the most reliable methods:

1. First, Build Your React Production Bundle

Before anything else, generate the optimized production build of your React app. Run this command in your React project directory:

npm run build
# Or if you use Yarn:
yarn build

This creates a build folder with all your minified, static assets ready to serve.

2. Serve the Build & Proxy API Requests to Your Backend

The key to avoiding code changes is ensuring your React app’s API requests (which should use relative paths like /api/endpoint) get routed to your local backend. The most seamless approach is using Express to handle both static serving and proxying:

Option A: Use Express (Full Control & Proxy Support)

This method lets you serve the static React files and proxy API requests in one server, eliminating cross-origin issues entirely.

  • Create a simple server.js file in your React project root:
const express = require('express');
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');

const app = express();

// Serve React's static build files
app.use(express.static(path.join(__dirname, 'build')));

// Proxy all /api requests to your local backend
app.use('/api', createProxyMiddleware({
  target: 'http://localhost:8000', // Replace with your backend's local URL/port
  changeOrigin: true,
}));

// Catch all other routes and return the React app (for client-side routing)
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Production React app running on http://localhost:${PORT}`);
});
  • Install the required dependencies:
npm install express http-proxy-middleware
  • Start the server:
node server.js

Now your React production app runs on http://localhost:3000, and all /api requests automatically forward to your backend—no changes needed to your React code.

3. Verify It Works

  • Make sure your local backend is running (e.g., on http://localhost:8000).
  • Open http://localhost:3000 in your browser—your production React app should load, and all API calls should successfully connect to your local backend without any code modifications.

Quick Note on Absolute API Paths

If your React app uses absolute paths (like http://localhost:8000/api) instead of relative ones, you still don’t need to change code—just ensure your backend is running on that exact URL/port. If you hit CORS errors, enable CORS on your backend or switch to the Express proxy method above.

内容的提问来源于stack exchange,提问作者JillAndMe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:13:52