错误:无法验证 nodejs 中的第一个证书 [英] Error: unable to verify the first certificate in nodejs

查看:69
本文介绍了错误:无法验证 nodejs 中的第一个证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 URL 从 jira 服务器下载文件,但出现错误.如何在代码中包含证书进行验证?

I'm trying to download a file from jira server using an URL but I'm getting an error. how to include certificate in the code to verify?

错误:

Error: unable to verify the first certificate in nodejs

at Error (native)
    at TLSSocket.<anonymous> (_tls_wrap.js:929:36)
   
  at TLSSocket.emit (events.js:104:17)

at TLSSocket._finishInit (_tls_wrap.js:460:8)

我的 Nodejs 代码:

var https = require("https");
var fs = require('fs');
var options = {
    host: 'jira.example.com',
    path: '/secure/attachment/206906/update.xlsx'
};

https.get(options, function (http_res) {
    
    var data = "";

  
    http_res.on("data", function (chunk) {
       
        data += chunk;
    });

   
    http_res.on("end", function () {
      
        var file = fs.createWriteStream("file.xlsx");
        data.pipe(file);
      
    });
});

推荐答案

尝试添加合适的根证书

这总是比盲目接受未经授权的端点更安全的选择,而后者只能作为最后的手段.

Try adding the appropriate root certificate

This is always going to be a much safer option than just blindly accepting unauthorised end points, which should in turn only be used as a last resort.

这可以像添加一样简单

require('https').globalAgent.options.ca = require('ssl-root-cas/latest').create();

到您的应用程序.

SSL Root CAs npm 包(此处使用)是一个非常关于这个问题的有用包.

The SSL Root CAs npm package (as used here) is a very useful package regarding this problem.

这篇关于错误:无法验证 nodejs 中的第一个证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆