首页 > 解决方案 > 连接到 ElasticSearch 时在 Node Js 应用程序上出现错误“错误:无法加载 PFX 证书”

问题描述

从 NodeJs 应用程序查询弹性时,我收到以下错误堆栈跟踪。

Error: Unable to load PFX certificate
at configSecureContext (node:internal/tls:304:15)
at Object.createSecureContext (node:_tls_common:113:3)
at Object.connect (node:_tls_wrap:1619:48)
at HttpsAgent.createConnection (node:https:136:22)
at HttpsAgent.createSocket (/Users/arshpreetsingh/Desktop/Desktop – MacBook Pro/Diagnotics Services/diagnostic-search/node_modules/agentkeepalive/lib/_http_agent.js:265:26)
at HttpsAgent.createSocket (/Users/arshpreetsingh/Desktop/Desktop – MacBook Pro/Diagnotics Services/diagnostic-search/node_modules/agentkeepalive/lib/agent.js:77:11)
at HttpsAgent.addRequest (/Users/arshpreetsingh/Desktop/Desktop – MacBook Pro/Diagnotics Services/diagnostic-search/node_modules/agentkeepalive/lib/_http_agent.js:239:10)
at new ClientRequest (node:_http_client:305:16)
at Object.request (node:https:317:10)
at Object.request (/Users/arshpreetsingh/Desktop/Desktop – MacBook Pro/Diagnotics Services/diagnostic-search/node_modules/agent-base/patch-core.js:25:22)

我的 elastc 客户端看起来像这样。

import elasticsearch from 'elasticsearch';
import CONFIG from '../config/index';

const elasticClient = new elasticsearch.Client({
host: CONFIG.es_url,
});


 export default {
    search: async (...args) => {
    try {
        return await elasticClient.search(...args)
    } catch (error) {
        if (error.status == 400) {
            return { hits: { total: 0 } }
        }
        throw error
    }
}

}

弹性搜索的 URL 以 https 开头。

标签: node.jshttpelasticsearchhttps

解决方案


有办法解决这个问题。</p>

  1. Node.js 版本 14.x
  2. 添加此配置 new elasticsearch.Client({ host: CONFIG.es_url, ssl:{ rejectUnauthorized: false } });

推荐阅读