node使用superagent库,请求时报错unable to verify the first certificate
表示客户端无法验证服务器提供的SSL证书链中的第一个证书
解决方法
在代码中直接加入
//禁用SSL证书验证
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
或
// 或者直接在代理请求时设置
https.globalAgent.options.rejectUnauthorized = false;
然后再进行请求即可解决
如问题无法解决
使用superagent关闭ssl验证报错
Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment varia
ble to ‘0’ makes TLS connections and HTTPS requests insecure by disabling certif
icate verification.
则可引入https禁止ssl验证
const superagent = require('superagent');
const https = require('https');
// 创建一个新的Agent,关闭rejectUnauthorized选项来禁用SSL验证
const agent = new https.Agent({
rejectUnauthorized: false
});
// 使用superagent并指定agent
superagent
.get('url')
.agent(agent)
.end((err, res) => {
if (err) {
console.error(err);
return;
}
console.log(res.text);
});