Node.js连接Elasticsearch出现连接拒绝错误,请求技术帮助
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
curlcommands 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 Errorwhen 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/elasticsearchpackage
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 9200to verify the port is in use - Windows: Run
netstat -ano | findstr :9200
- Linux/macOS: Run
- Check your
elasticsearch.ymlconfig file:- Ensure
http.port: 9200is set - Ensure
network.hostallows localhost access (default is127.0.0.1, which should work)
- Ensure
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

