Skip to content

Connecting

The Aerospike Node.js client can connect and periodically ping nodes for cluster status.

To connect to the Aerospike cluster, you must:

  • Import the module into your application.
  • Configure a client instance.
  • Connect to the cluster.

Import the Module

To enable the Aerospike Node.js client, import the module into your application:

const Aerospike = await import("aerospike");

Creating a Client

Before making any database operation, you must configure a client object, and successfully connect to the database.

To create a client, initialize the client using a configuration object and specify the following options:

  • user — The username to login to the Aerospike cluster (only available in security-feature clusters).
  • password — The password for the username (only available in security-feature clusters).
  • hosts — Specify an array of hosts to connect to. The client iterates over the list until it successfully connects with a server.
  • log — Specify the log verbosity level and redirect log messages to a file.
  • policies — Specify client default behavior.

This example shows how to connect to an Aerospike cluster with the default authentication method:

const client = Aerospike.client({
hosts: [{ addr: "127.0.0.1", port: 3000 }],
log: {
level: aerospike.log.INFO,
},
});

The following example shows how to connect to an Aerospike cluster with the PKI authentication method:

const client = Aerospike.client({
hosts: [{ addr: "127.0.0.1", port: 4333 }],
tls: {
cafile: process.env.CAFILE,
keyfile: process.env.KEYFILE,
certfile: process.env.CERT,
},
authMode: Aerospike.auth.AUTH_PKI,
log: {
level: aerospike.log.INFO,
},
});

On successful client creation, you can connect and execute operations in the cluster.

Connecting to the Cluster

To connect to the Aerospike cluster:

await client.connect();

Your application is ready to execute database operations.

TLS secured connection

TLS connections require certificate configuration on both client and server. The TLS connection port is usually set to 4333 instead of the typically unsecured port 3000. See TLS configuration for more information about setting up TLS on your Aerospike Database server.

The client TLS name (clusterTLSName in this example) should be equal to either the certificate common name (CN) or the certificate subject alternative name. Usually, the client TLS name is also the server’s tls-name configuration value.

TLS can also be configured to limit protocols/ciphers and revoke certificates. See documentation for client TLS configuration options.

// Usually, the client tlsname matches the server's "tls-name" server configuration
const hosts = [
{ addr: "192.168.10.10", port: 4333, tlsname: "clusterTLSName" },
];
// Create tlsInfo object
const tlsInfo = {
// TLS must be enabled here
enable: true,
// Set optional parameters to configure TLS as needed
// Path to a trusted CA certificate file
// cafile: '/etc/aerospike/ssl/root.crt',
// Path to the client's certificate chain file for mutual authentication
// certfile: '/etc/aerospike/ssl/client.crt',
// Path to the client's key for mutual authentication
// keyfile: '/etc/aerospike/ssl/client.key.pem'
};
const config = {
hosts,
tls: tlsInfo,
};
const client = await Aerospike.connect(config);

TLS secured connection with PKI authentication

TLS connections can be extended with PKI authentication. PKI user authentication specifies that the username is located directly in the client certificate’s common name (CN). PKI requires that mutual authentication (both client and server validate each other’s certificates) is configured on the server. The server must examine the client’s certificate to obtain the username. When PKI authentication is enabled, the CN of the user’s (client) certificate must match their username.

// Usually, the client tlsname matches the server's "tls-name" server configuration
const hosts = [
{ addr: "192.168.10.10", port: 4333, tlsname: "clusterTLSName" },
];
// Create tlsInfo object
const tlsInfo = {
// TLS must be enabled here
enable: true,
// Set optional parameters to configure TLS as needed
// Path to a trusted CA certificate file
// cafile: '/etc/aerospike/ssl/root.crt',
// Path to the client's certificate chain file for mutual authentication
// certfile: '/etc/aerospike/ssl/client.crt',
// Path to the client's key for mutual authentication
// keyfile: '/etc/aerospike/ssl/client.key.pem'
};
const config = {
hosts,
tls: tlsInfo,
// To enable PKI authentication, add AUTH_PKI as the authMode
authMode: Aerospike.auth.AUTH_PKI,
};
const client = await Aerospike.connect(config);

Compression

You can enable data compression on both the client side and the server side. The Java client uses Zstandard (ZLib) for compression. For information about configuring compression on the server side, see Configuring Storage Compression.

const key = new Aerospike.Key("sandbox", "ufodata", 5001);
const bins = { example: 10 };
const metadata = null;
const writePolicy = new Aerospike.WritePolicy({ compress: true });
await client.put(key, bins, metadata, writePolicy);
const readPolicy = new Aerospike.readPolicy({ compress: true });
const record = await client.get(key);
Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?