创建react应用程序,使Node.js请求不通过代理
我目前正在编写一个带有Electron、CreateReact应用程序和Node.js的应用程序(注意:在Electron中,webSecurity被禁用以发出CORS请求)。 在我的App.js文件中,我试图编写一个模块,通过向站点发送请求并检查响应来测试代理,如下所示创建react应用程序,使Node.js请求不通过代理,node.js,reactjs,request,electron,create-react-app,Node.js,Reactjs,Request,Electron,Create React App,我目前正在编写一个带有Electron、CreateReact应用程序和Node.js的应用程序(注意:在Electron中,webSecurity被禁用以发出CORS请求)。 在我的App.js文件中,我试图编写一个模块,通过向站点发送请求并检查响应来测试代理,如下所示 var proxiedRequest=request.defaults({'proxy':"http://username:password@test.test.com:0000"}) await proxiedRequest
var proxiedRequest=request.defaults({'proxy':"http://username:password@test.test.com:0000"})
await proxiedRequest.get(site, function(error, resp, body){
console.log(resp.statusCode)
console.log(resp)
}
现在,我创建了一个单独的test.js文件,用于确保问题是由它引起的
const request = require('request-promise')
var proxyUrl = //working proxy here;
var proxiedRequest = request.defaults({'proxy': proxyUrl});
proxiedRequest.get('http://google.com',function(err, resp, body){
console.log(err)
console.log(resp.statusCode)
})
我用工作代理和非工作代理对它进行了测试,得到了正确的回答
我的问题是为什么create-react-app导致请求不能通过代理路由?这是因为它被托管在或实际上是一个基于电子的问题吗?您必须将您的本地主机添加到后端的白标签cors中。如果您的API由express制作,您可以使用cors lib解决此问题