node.js請求HTTPS報錯:UNABLE_TO_VERIFY_LEAF_SIGNATURE\的解決方法
更新時間:2016年12月18日 15:46:56 投稿:daisy
最近在工作中遇到一個問題,node.js請求HTTPS時報錯:Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE\,通過查找網上的一些資料找到了解決方法,現(xiàn)在總結下分享給大家,有需要的朋友們可以參考借鑒,下面來一起看看吧。
發(fā)現(xiàn)錯誤
最近在用Nodejs發(fā)送https請求時候,出現(xiàn)\”Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE\”的錯誤,錯誤如下:
events.js:72 throw er; // Unhandled \'error\' event ^ Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE at SecurePair. (tls.js:1381:32) at SecurePair.emit (events.js:92:17) at SecurePair.maybeInitFinished (tls.js:980:10) at CleartextStream.read [as _read] (tls.js:472:13) at CleartextStream.Readable.read (_stream_readable.js:341:10) at EncryptedStream.write [as _write] (tls.js:369:25) at doWrite (_stream_writable.js:226:10) at writeOrBuffer (_stream_writable.js:216:5) at EncryptedStream.Writable.write (_stream_writable.js:183:11) at write (_stream_readable.js:602:24)
錯誤的原因是:對方數(shù)字證書設置不正確,
解決辦法: 將rejectUnauthorized參數(shù)設置成false
var https = require(\'https\');
var options = {
hostname: \'www.magentonotes.com\',
port: 443,
path: \'/\',
method: \'GET\',
rejectUnauthorized:false
};
var req = https.request(options, function(res) {
console.log(\"statusCode: \", res.statusCode);
console.log(\"headers: \", res.headers);
res.on(\'data\', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on(\'error\', function(e) {
console.error(e);
});
參考資料:https://nodejs.org/api/https.html
總結
以上就是關于node.js請求HTTPS報錯的解決方法,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關文章
推薦 21 款優(yōu)秀的高性能 Node.js 開發(fā)框架
Node.js是JavaScript中最為流行的框架之一,易于創(chuàng)建可擴展的Web應用。Node.js包含不同類型框架,包括MVC, full-stack,REST API以及Generators。借助這些框架使Node.js更加易于使用,它還支持眾多特性功能,只需幾個步驟就可快速搭建強大的Web應用。本文為大家推薦21款2014-08-08

