简体   繁体   English

获取CORS节点http代理以响应200个选项

[英]Getting CORS node-http-proxy to respond 200 to OPTIONS

I am trying to get a web page running on :9000 to make a request to a reverse proxy running in node on :8080 to accept requests as if it was on the same domain. 我试图让网页在:9000上运行,以向在:8080的节点上运行的反向代理发出请求,以接受请求,就好像它在同一域中一样。 I have learned that a different port == a different domain, thus OPTIONS requests are sent. 我了解到,不同的端口==不同的域,因此发送了OPTIONS请求。 My server comes back with 401 unauthorized, and angular's http client gets the response with no data, no code with a console error "invalid http status code 401 unauthorized". 我的服务器返回了401未经授权,而angular的http客户端收到的响应中没有数据,没有控制台错误代码“无效的HTTP状态代码401未经授权”。

I just want the reverse proxy to act as if it was on the same domain and say "OK" to these options requests. 我只希望反向代理像在同一个域中一样对这些选项请求说“确定”。

It is the responsibility of the reverse proxy to respond to the OPTIONS request with the expected CORS headers. 反向代理负责使用预期的CORS标头响应OPTIONS请求。 Can you provide more details on the node reverse proxy. 您能否提供有关节点反向代理的更多详细信息。 Is it express based ? 它是基于快递的吗?

These are the headers you need to respond with (your use case may vary but this is what I needed in a recent project): 这些是您需要响应的标头(您的用例可能会有所不同,但这是我在最近的项目中需要的标头):

res.setHeader('Access-Control-Allow-Origin', 'whatever.com:9000');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true); // you probably dont want this one unless there is auth/cookies involved
res.setHeader('Access-Control-Allow-Methods', 'GET,PATCH,POST,PUT,DELETE');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM