头图

The source code is as follows: use the method createProxyServer to create a proxy server, listen on port 8082, and send the request to the server listening on localhost:9000.

The latter simply returns a request successfully proxied message to the requester.

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);

Encountered the error message:

Access to XMLHttpRequest at 'http://localhost:8082/https://services.odata.org/V2/Northwind/Northwind.svc/$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.

solution

Add a field to the HTTP response header field: "Access-Control-Allow-Origin": "*"

After restarting the proxy server, the error message became:

Access to XMLHttpRequest at 'http://localhost:8082/https://services.odata.org/V2/Northwind/Northwind.svc/$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.

The solution is to add the following fields to the response:

res.writeHead(200, { 'Content-Type': 'text/plain',
                        "Access-Control-Allow-Origin": "*",
                        "Access-Control-Allow-Headers": "*" });

The problem disappeared after modification:

The metadata data request initiated by the SAP UI5 application was successfully intercepted by the proxy server and returned:

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',

This error is related to this StackOverflow discussion.

The code is also similar:

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!');
});

When the request is sent to idena, the proxy server tries to send to the server:

The source server is HTTP and the destination server is HTTPS.

The solution to this error: In the proxy option, specify the following option:

changeOrigin: true

After restarting the proxy server, the problem disappeared: ERR_TLS_CERT_ALTNAME_INVALID bug was fixed.

But this error message comes back:

http://localhost:8082/https://services.odata.org/V2/Northwind/Northwind.svc/$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:

Go directly to this url: http://localhost:8082/https://services.odata.org/V2/Northwind/Northwind.svc/ $metadata?sap-language=EN

The following error will be encountered:

More Jerry's original articles, all in: "Wang Zixi":


注销
1k 声望1.6k 粉丝

invalid