从同一域服务React应用和后端服务器 [英] Serve React app and backend server from same domain

查看:109
本文介绍了从同一域服务React应用和后端服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自" https://www.domain1.com "和后端的应用程序在其他域" https://www.domain2.com "上提供.从domain1.com到domain2.com的Api请求不是简单的HTTP请求,因为我们在所有请求中都添加了Authorization标头.这导致提出了CORS OPTIONS请求,从而进一步增加了应用程序的延迟. 1.有什么办法可以合并两个域? 2.如果我将域分开,是否可以避免针对这些非简单HTTP请求的CORS OPTIONS预检请求?

I have a react app serving from "https://www.domain1.com" and the backend is served on a different domain "https://www.domain2.com". Api requests made from domain1.com to domain2.com are not simple HTTP requests since we are adding an Authorization header in all the requests. This leads to CORS OPTIONS request being made further increasing the latency of the app by a heavy margin. 1. Is there any way I can merge the 2 domains ? 2. Can I avoid CORS OPTIONS preflight requests for these non-simple HTTP requests if I keep the domains separate ?

推荐答案

是的,您可以避免在后端而不是在前端使用CORS Policy选项.在开发API的地方,必须在那里允许CORS政策.允许后,您可以调用从其他域提供的API.

Yes, you can avoid CORS Policy option in your backend side not in your frontend. Where the API is developed, CORS Policy must be allowed there. After allowing you can call API which is serving from other domain.

这篇关于从同一域服务React应用和后端服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆