跨域解决方案 http-proxy
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跨域解决方案 http-proxy相关的知识,希望对你有一定的参考价值。
参考技术A 跨域解决方案:本文将讲解方案4和方案7
运行service.js 测试接口
测试请求接口
在浏览器中请求 http://localhost:8080/ 返回 I'm service A 则表示请求成功了.
使用vscode并开启 Live Server 插件, 在vscode中打开index.html页面, 点击右下角Live Server图标即可在默认端口启动 http://localhost:5500 , 此处也是为了默认远程请求页面, 注意如果使用其他代理地址, 请修改下面的位置
打开网页 http://127.0.0.1:5500/index.html
打开网页 http://localhost:3000/index.html
如果 3000/index.html 也能请求则说明该方法成功了.
启动nginx并重载配置文件
本文github地址: https://github.com/wwmin/http_proxy_node
节点 http-proxy:将流量转发到外部 https 站点 - 崩溃
【中文标题】节点 http-proxy:将流量转发到外部 https 站点 - 崩溃【英文标题】:Node http-proxy: forward traffic to external https site - crashes 【发布时间】:2017-10-24 20:56:58 【问题描述】:我想将 REST 请求从我的前端 wepp 应用程序传输到外部 Jira 服务器上的 API。
为此,我使用的是节点 http-proxy,这对于 http 的 Jira 服务器来说是可以的。
但现在我想为 https 创建一个单独的服务器。
所以对this 示例进行一些更改,我现在有了这个:
var path = require('path'),
fs = require('fs'),
httpProxy = require('http-proxy'),
certFolder = '/my/cert/folder';
//
// Create the HTTPS proxy server listening on port 8002
//
httpProxy.createServer(
//(placeholder address)
target:
host: 'https://ext.jiraserver.com',
port: 443
,
// letsencrypt cert
ssl:
key: fs.readFileSync(path.join(certFolder, 'privkey.pem'), 'utf8'),
cert: fs.readFileSync(path.join(certFolder, 'fullchain.pem'), 'utf8')
,
secure: true
).listen(8002);
console.log('https proxy server started on port 8002');
但是当我向我的服务器发出请求时,https://my.domain.com:8002 它会使服务器崩溃并显示错误消息:
.../node_modules/http-proxy/lib/http-proxy/index.js:119 抛出错误; ^
错误:getaddrinfo ENOTFOUND https://ext.jiraserver.com https://ext.jiraserver.com:443 在 errnoException (dns.js:28:10) 在 GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
我似乎无法让它工作......服务器在线并且地址正确,所以我不知道出了什么问题。
是我的代码有问题还是我可以做些什么来让它工作?
谢谢!
【问题讨论】:
【参考方案1】:不要在 DNS 主机定义中包含 https://
。
host: 'ext.jiraserver.com',
错误消息告诉您这是 DNS 解析问题。您正在尝试查找 https://ext.jiraserver.com
的 DNS,包括 https
。
【讨论】:
我试过了,我只得到:400 Bad Request - 普通的 HTTP 请求被发送到 HTTPS 端口。感觉就像 httpProxy 启动了一个 http-server,而不是 https? @mottosson 尝试将https: true
添加到target
配置中。以上是关于跨域解决方案 http-proxy的主要内容,如果未能解决你的问题,请参考以下文章