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:

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)

My Nodejs code:

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.

This can be as simple as adding

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

to your application.

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

Node.js相关问答推荐

用于SLACK命令返回json而不是文本的AWS lambda函数

在编译时强制不缩小类型范围

有没有办法判断 UUID 是否是使用 node.js 中的特定命名空间生成的?

安装样式组件时出现react 错误

Zod 模式中的self 数组

Mongodb聚合传递一个匹配的数组和一个不匹配的数组

类型中缺少属性,该类型是由类实现的接口

如何以编程方式检测nodejs中的调试模式?

异步函数 - 等待不等待promise

使用 node.js 执行一个 exe 文件

容器之间的 Docker HTTP 请求

使用 Socket.io 将客户端连接到服务器

如何在 Node.js 中使用 chmod

为什么 Node 控制台不显示功能代码?

__dirname 未在 Node 14 版本中定义

在单独的模块中定义 Mongoose 模型

Mongoose - 保存字符串数组

将 Heroku App 连接到 Atlas MongoDB 云服务

未在 Windows 8.1 上构建的 node 包 - 缺少 Microsoft.Cpp.Default.props

我可以让 node --inspect 自动打开 Chrome