乐趣区

关于node.js:windows-环境下使用-Nodejs-遇到-unable-to-get-local-issuer-certificate

我有一个 OData 服务,部署在 SAP S/4HANA 服务器上,浏览器里通过如下 url,能够失常拜访到其元数据:

我编写了一个 node.js 利用,通过编程的形式拜访这个 OData 服务的元数据:

var request = require('request');

var url = 'https://ldai2xxx:44356/sap/opu/odata/sap/ZBOOK_MANAGE_SRV/$metadata';

var oOptions = {
        url: url,
        method: 'GET'
    };

var oPromise = new Promise(function(resolve,reject){request.get(oOptions,function(error,response,body){if(error){console.log("error occurred:" + error);
              reject(error);
        }
        resolve(body);
    });
});

oPromise.then((data) => console.log(data));

应用 node 执行下面的代码,遇到如下谬误音讯:

error occurred: Error: unable to get local issuer certificate
(node:33376) UnhandledPromiseRejectionWarning: Error: unable to get local issuer certificate

at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:932:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)

(Use node --trace-warnings ... to show where the warning was created)
(node:33376) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.ht…). (rejection id: 1)
(node:33376) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

在应用程序生成过程中,当用户连贯的零碎应用 SSL 反对平安 HTTPS 流量时,可能会呈现有效平安证书谬误。在某些状况下,证书是应用用户操作系统未知的本地证书颁发机构生成的。如果产生这种状况,应用程序生成器会回绝连贯申请并报告谬误。

解决方案

windows 环境下设置环境变量,应用 set 语法,命名后不加空格,间接附上两个 &&, 而后空格,跟上新的命令。

set NODE_TLS_REJECT_UNAUTHORIZED=0&& node ag3.js

这种解决方案的用意是,设置环境变量 NODE_TLS_REJECT_UNAUTHORIZED 的值为 0,临时禁用 SSL certificate 的校验。

执行之后,看到正告音讯:

Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to ‘0’ makes TLS connections and HTTPS requests insecure by disabling certificate verification.

提醒咱们,这个环境变量的设置,禁掉了 certificate 验证,让 TLS 连贯和 HTTPS 申请变得不再平安。

收到 401 状态码,阐明与服务器的连贯终于通了:

退出移动版