惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
L
LangChain Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
G
Google Developers Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - zyip

超越2028:我们这一代,是末代普通程序员 中国生成式AI民用开荒史:2022–2023 icon资源 python几类安装方法 pyautogui 深入理解 CSS 中的 BFC wpf mvvm(prism) wpf 打包成单文件 vite 读取router mysql ensp run Python asyncio code in a Jupyter notebook 实现两个socket server 黑苹果 Sublime Text4 4143版本激活 comfyUI相关 wpf data binding windows命令行批量给文件名加前缀 unable to resolve dependency tree
React17 , CRA项目添加Access-Control-Allow-Origin header
zyip · 2025-04-14 · via 博客园 - zyip

In a micro frontend project, unfortunately, I'm facing an issue. I want to access my React 17 project as a sub - project in my parent project, but it's blocked by the Cross - origin policy.

Thankfully, there's a solution. I configure the Create React App (CRA) development server to send the Access - Control - Allow - Origin header directly. This simple yet effective configuration is extremely useful if you want to expose your development server as a simple API endpoint for other clients to access without a separate backend server during development.

Here's how you can do it using the src/setupProxy.js file:

  1. Create src/setupProxy.js (if it doesn't exist): In your project's src directory, create a file named setupProxy.js.

  2. Add the following code:

  3. Restart your dev server

\src\setupProxy.js  

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/', // Apply to all routes of the dev server
    (req, res, next) => {
      res.setHeader('Access-Control-Allow-Origin', '*'); // Allow requests from any origin
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); // Specify allowed methods
      res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // Specify allowed headers
      next();
    }
  );
};