使快递服务器接受CORS请求

编程入门 行业动态 更新时间:2024-10-28 08:27:43
本文介绍了使快递服务器接受CORS请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的快递服务器在 http:// localhost:3000 上运行(我称之为这个网络服务器)我有另一个应用程序运行在本地主机:8100(我称之为简单的应用程序)

I have my express server running on localhost:3000 (I call this web-server) I have another application running on localhost:8100 (I call this simply 'app')

当我的应用程序拨打网络服务器时,我收到消息: / p>

When my app makes a call to the webserver I receive the message:

"XMLHTTPReqeust cannot load 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 'localhost:81000' is therefore not allowed acecss"

此消息显示在浏览器控制台中。

This message shows up in the browser console.

我已经设置在我的节点webserver的中间件中的以下选项

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', 'localhost:8100');

但是这不能解决问题。

推荐答案

我个人更喜欢 cors 模块。代码很简单:

I personally prefer the cors module. The code is really simple:

var whitelist = [ '0.0.0.0:3000', ]; var corsOptions = { origin: function(origin, callback){ var originIsWhitelisted = whitelist.indexOf(origin) !== -1; callback(null, originIsWhitelisted); }, credentials: true }; app.use(cors(corsOptions));

更多推荐

使快递服务器接受CORS请求

本文发布于:2023-10-09 16:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476220.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快递   服务器   CORS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!