共计 2780 个字符,预计需要花费 7 分钟才能阅读完成。
源代码如下:应用办法 createProxyServer
创立一个代理服务器, 监听在端口 8082 上,把申请发送给 localhost:9000 上监听的服务器。
后者仅仅返回一个 request successfully proxied 的音讯给申请方。
var http = require('http'),
httpProxy = require('http-proxy');
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8082);
http.createServer(function (req, res) {res.writeHead(200, { 'Content-Type': 'text/plain'});
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();}).listen(9000);
遇到谬误音讯:
Access to XMLHttpRequest at ‘http://localhost:8082/https:/…$metadata?sap-language=EN’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
解决方案
在 HTTP response 头部字段削减一条字段:”Access-Control-Allow-Origin”: “*”
重启 proxy 服务器之后,谬误音讯变成了:
Access to XMLHttpRequest at ‘http://localhost:8082/https:/…$metadata?sap-language=EN’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Request header field maxdataserviceversion is not allowed by Access-Control-Allow-Headers in preflight response.
解决办法是,在 response 里增加如下字段:
res.writeHead(200, { 'Content-Type': 'text/plain',
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "*" });
批改之后问题隐没:
SAP UI5 利用发动的 metadata 数据申请,被 proxy 服务器胜利拦挡并返回:
Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate’s altnames: Host: localhost. is not in the cert’s altnames: DNS:.azurewebsites.net, DNS:.scm.azurewebsites.net, DNS:.azure-mobile.net, DNS:.scm.azure-mobile.net, DNS:*.sso.azurewebsites.net
at Object.checkServerIdentity (tls.js:287:12)
at TLSSocket.onConnectSecure (_tls_wrap.js:1511:27)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:936:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:710:12) {
reason: “Host: localhost. is not in the cert’s altnames: DNS:.azurewebsites.net, DNS:.scm.azurewebsites.net, DNS:.azure-mobile.net, DNS:.scm.azure-mobile.net, DNS:*.sso.azurewebsites.net”,
host: ‘localhost’,
这个谬误和这个 StackOverflow 探讨相干。
代码也相似:
var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
var serverOne = 'https://idena.navarra.es/ogc/wms?';
app.all('/idena', function (req, res) {apiProxy.web(req, res, {target: serverOne});
});
app.listen(3000, function () {console.log('Working!');
});
当申请发送给 idena 时,代理服务器试图发送往服务器:https://idena.navarra.es/ogc/…
源服务器是 HTTP,而目标服务器是 HTTPS.
解决这个谬误的方法:在 proxy option 里,指定下列这个选项:
changeOrigin: true
从新 proxy 服务器之后,问题隐没:ERR_TLS_CERT_ALTNAME_INVALID
谬误被修复了。
然而这个谬误音讯又回来了:
http://localhost:8082/https:/…$metadata?sap-language=EN’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
400 bad request:
间接拜访这个 url:http://localhost:8082/https:/…$metadata?sap-language=EN
会遇到如下谬误:
更多 Jerry 的原创文章,尽在:” 汪子熙 ”: