Skip to main content

Connecting with the Aerospike Node.js Client

The Aerospike Nodejs 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 = require('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:

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

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

let 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:

client.connect(function (error) {
if (error) {
// handle failure
console.log('Connection to Aerospike cluster failed!')
} else {
// handle success
console.log('Connection to Aerospike cluster succeeded!')
}
})

Your application is ready to execute database operations.