nodejs请求HTTPS报错的解决方法

by Web全栈工程师 on 2015 年 10 月 03 日

在用Nodejs发送https请求时候,出现\”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)

错误的原因是:对方数字证书设置不正确,

解决办法: 将rejectUnauthorized参数设置成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

Comments on this entry are closed.

Previous post:

Next post: