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 = 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.
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.
const Aerospike = require('aerospike')
// Usually, the client tlsname matches the server's "tls-name" server configurationlet hosts = [{ addr: "192.168.10.10", port: 4333, tlsname: 'clusterTLSName'}]
let 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'}
let config = { hosts: hosts, tls: tlsInfo}
;(async function () { let client; try { client = await Aerospike.connect(config) } catch(error){ console.log(error) } finally{ await client.close() }})()
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.
const Aerospike = require('aerospike')
// Usually, the client tlsname matches the server's "tls-name" server configurationlet hosts = [{ addr: "192.168.10.10", port: 4333, tlsname: 'clusterTLSName'}]
let 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'}
let config = { hosts: hosts, tls: tlsInfo,
// To enable PKI authentication, add AUTH_PKI as the authMode authMode: Aerospike.auth.AUTH_PKI}
;(async function () { let client; try { client = await Aerospike.connect(config) } catch(error){ console.log(error) } finally{ await client.close() }})()
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 Aerospike = require('aerospike')
let hosts = [{ addr: "192.168.10.10", port: 3000 }]
let config = { hosts: hosts}
const sightingString = `{ "sighting": { "occurred": 20200912, "reported": 20200916, "posted": 20201105, "report": { "city": "Kirkland", "duration": "~30 minutes", "shape": ["circle"], "state": "WA", "summary": "4 rotating orange lights in the Kingsgate area above the Safeway. Around 9pm the power went out in the Kingsgate area. Four lights were spotted rotating above the local Safeway and surrounding streets. They were rotating fast but staying relatively in the same spots. Also described as orange lights. About thirty minutes later they disappeared. The second they disappeared the power was restored. Later a station of police from Woodinville and Kirkland came to guard the street where it happened. They wouldn't let anyone go past the street, putting out search lights and flare signals so people couldn't drive past Safeway. The police also would not let people walk past to go home." }, "location": "{\\\"type\\\":\\\"Point\\\",\\\"coordinates\\\":[-122.1966441,47.69328259]}" }}`;
// Parse the main stringlet parsed = JSON.parse(sightingString);parsed.sighting.location = JSON.parse(parsed.sighting.location);
let compressExampleKey = Aerospike.Key("sandbox", "ufo", 1)
;(async function () { let client; try { client = await Aerospike.connect(config)
let writePolicy = new Aerospike.WritePolicy({compress: true})
let result = await client.put(compressExampleKey, {sighting: parsed}, {}, writePolicy)
let readPolicy = new Aerospike.ReadPolicy({compress: true})
let record = await client.get(compressExampleKey, readPolicy)
} catch(error){ console.log(error) } finally{ await client.close() }})()