让快递服务器接受 CORS 请求 [英] Getting express server to accept CORS request
问题描述
我的快速服务器在 http://localhost:3000 上运行(我称之为网络服务器)我在 localhost:8100 上运行了另一个应用程序(我将其简称为应用程序")
I have my express server running on http://localhost:3000 (I call this web-server) I have another application running on localhost:8100 (I call this simply 'app')
当我的应用程序调用网络服务器时,我会收到消息:
When my app makes a call to the webserver I receive the message:
"XMLHTTPReqeust 无法加载 http://localhost:3000/auth/facebook.对预检请求的响应未通过访问控制检查.当凭证标志为真时,不能在Access-Control-Allow-Origin"中使用通配符*".因此,Origin 'http://localhost:81000' 是不允许访问的"
"XMLHTTPReqeust cannot load http://localhost:3000/auth/facebook. Response to preflight request doesn't pass access control check. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' when the credentials flag is true. Origin 'http://localhost:81000' is therefore not allowed acecss"
此消息显示在浏览器控制台中.
This message shows up in the browser console.
我在节点网络服务器的中间件中设置了以下选项
I have setup the following options in the middleware of my node webserver
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT, POST,DELETE');
在阅读了几个stackoverfow问题后,我还添加了以下内容:
After reading few stackoverfow questions, I also added the following:
res.header('Access-Control-Allow-Origin', 'http://localhost:8100');
但这并不能解决问题.
推荐答案
我是用cors实现的,很简单
I use cors and implement it so, it's very simple
var cors=require('cors');
app.use(cors({origin:true,credentials: true}));
这篇关于让快递服务器接受 CORS 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!