You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js连接Elasticsearch出现连接拒绝错误,请求技术帮助

Troubleshooting Node.js Elasticsearch Connection Issues

Hey there, let's break down what's going wrong with your setup and fix it step by step.

First, Clear Up the Port Confusion

Elasticsearch uses two distinct ports for different purposes:

  • 9200: This is the HTTP REST API port — your curl commands are already using this successfully to interact with Elasticsearch, so we know this port works.
  • 9300: This is the TCP port reserved for inter-node cluster communication. It's not designed to handle HTTP requests, which is why you got a Parse Error when trying to connect here. We need to stick to port 9200 for your Node.js client.

Why Did the Port 9200 Connection Fail?

Your curl tests confirm Elasticsearch is running and listening on 9200, so the ECONNREFUSED error likely comes from one of these common issues:

1. Outdated Elasticsearch Client Package

The old elasticsearch npm package is no longer maintained and has compatibility gaps with modern Elasticsearch versions (7.x and above). The official, supported alternative is @elastic/elasticsearch.

2. Version Mismatch Between Client and Elasticsearch

If you're set on using the old client, it might not support the version of Elasticsearch you're running:

  • Elasticsearch 7.x requires the old client v16.x
  • Elasticsearch 8.x only works with the new @elastic/elasticsearch package

Fixes to Try

Option 1: Switch to the Official Supported Client

First, replace the old package with the official one:

npm uninstall elasticsearch
npm install @elastic/elasticsearch

Then update your code to match the new client's API (we'll use async/await for cleaner error handling):

const { Client } = require('@elastic/elasticsearch');

// Initialize client with the correct HTTP port (9200)
const client = new Client({
  node: 'http://localhost:9200',
  requestTimeout: Infinity,
  keepAlive: true
});

// Check connection with async/await
async function verifyESConnection() {
  try {
    await client.ping({
      requestTimeout: Infinity,
      hello: "elasticsearch!"
    });
    console.log('All is well');
  } catch (error) {
    console.trace('elasticsearch cluster is down!');
    console.error('Detailed error:', error);
  }
}

verifyESConnection();

Option 2: Fix the Old Client (If You Must Keep It)

If you want to stick with the legacy elasticsearch package:

  • Install a version compatible with your Elasticsearch instance (check the package's documentation for version mappings)
  • Explicitly add http:// to your host configuration (some older client versions require this):
    var elasticsearch = require('elasticsearch');
    var client = new elasticsearch.Client({
      host: 'http://localhost:9200', // Add http:// to avoid connection issues
      log: 'trace',
      requestTimeout: Infinity,
      keepAlive: true
    });
    
    client.ping({
      requestTimeout: Infinity,
      hello: "elasticsearch!"
    }, function (error) {
      if (error) {
        console.trace('elasticsearch cluster is down!');
      } else {
        console.log('All is well');
      }
    });
    
  • Wait a few seconds after starting Elasticsearch before running your Node.js code — sometimes the HTTP endpoint takes a moment to initialize fully.

Additional Checks

  • Confirm Elasticsearch is listening on 9200:
    • Linux/macOS: Run netstat -an | grep 9200 to verify the port is in use
    • Windows: Run netstat -ano | findstr :9200
  • Check your elasticsearch.yml config file:
    • Ensure http.port: 9200 is set
    • Ensure network.host allows localhost access (default is 127.0.0.1, which should work)

Since your curl commands work perfectly, switching to the official client or fixing the legacy client's configuration should resolve the connection issues.

内容的提问来源于stack exchange,提问作者ankitkhandelwal185

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:41:59