What is @elastic/transport?
@elastic/transport is a low-level HTTP transport library for Node.js, designed to be used with Elasticsearch clients. It provides the core functionality for making HTTP requests to Elasticsearch clusters, handling retries, and managing connections.
What are @elastic/transport's main functionalities?
Basic HTTP Request
This feature allows you to make basic HTTP requests to an Elasticsearch cluster. The code sample demonstrates how to create a Transport instance and make a GET request to the root endpoint of the Elasticsearch cluster.
const { Transport } = require('@elastic/transport');
const transport = new Transport({
node: 'http://localhost:9200'
});
async function makeRequest() {
const response = await transport.request({
method: 'GET',
path: '/'
});
console.log(response.body);
}
makeRequest();
Handling Retries
This feature allows you to configure the number of retries for failed requests. The code sample demonstrates how to set up a Transport instance with a maximum of 3 retries and handle the request failure.
const { Transport } = require('@elastic/transport');
const transport = new Transport({
node: 'http://localhost:9200',
maxRetries: 3
});
async function makeRequest() {
try {
const response = await transport.request({
method: 'GET',
path: '/'
});
console.log(response.body);
} catch (error) {
console.error('Request failed after 3 retries:', error);
}
}
makeRequest();
Connection Pooling
This feature allows you to manage multiple connections to different nodes in an Elasticsearch cluster. The code sample demonstrates how to create a Transport instance with a pool of nodes and make a request.
const { Transport } = require('@elastic/transport');
const transport = new Transport({
nodes: [
'http://localhost:9200',
'http://localhost:9201'
]
});
async function makeRequest() {
const response = await transport.request({
method: 'GET',
path: '/'
});
console.log(response.body);
}
makeRequest();
Other packages similar to @elastic/transport
axios
Axios is a promise-based HTTP client for the browser and Node.js. It provides a simple API for making HTTP requests and handling responses. Compared to @elastic/transport, Axios is more general-purpose and not specifically designed for Elasticsearch.
got
Got is a human-friendly and powerful HTTP request library for Node.js. It supports retries, connection pooling, and other advanced features. While Got is versatile and robust, it is not specialized for Elasticsearch like @elastic/transport.
node-fetch
Node-fetch is a lightweight module that brings `window.fetch` to Node.js. It is minimalistic and focuses on providing a fetch API in Node.js environments. Unlike @elastic/transport, node-fetch does not include advanced features like retries and connection pooling out of the box.
Elastic Node.js Transport
This is a HTTP transport Node.js library for communicate with Elastic products,
like Elasticsearch.
Install
npm install @elastic/transport
Node.js support
NOTE: The minimum supported version of Node.js is v16
.
The client versioning follows the Elastc Stack versioning, this means that
major, minor, and patch releases are done following a precise schedule that
often does not coincide with the Node.js release times.
To avoid support insecure and unsupported versions of Node.js, the
client will drop the support of EOL versions of Node.js between minor releases.
Typically, as soon as a Node.js version goes into EOL, the client will continue
to support that version for at least another minor release.
Unless you are always using a supported version of Node.js,
we recommend defining the client dependency in your
package.json
with the ~
instead of ^
. In this way, you will lock the
dependency on the minor release and not the major. (for example, ~7.10.0
instead
of ^7.10.0
).
Node.js Version | Node.js EOL date | End of support |
---|
8.x | December 2019 | 7.11 (early 2021) |
10.x | April 2021 | 7.12 (mid 2021) |
12.x | April 2022 | 8.2 (early 2022) |
14.x | April 2023 | 8.8 (early 2023) |
API
Usage
License
This software is licensed under the Apache 2 license.