Getting Started
suggest changenode_redis, as you may have guessed, is the Redis client for Node.js. You can install it via npm using the following command.
npm install redis
Once you have installed node_redis module you are good to go. Let’s create a simple file, app.js, and see how to connect with Redis from Node.js.
app.js
var redis = require('redis');
var client = redis.createClient(); //creates a new client
By default, redis.createClient() uses 127.0.0.1 and port 6379. Pass a different host and port as follows. This createClient(port, host) form is node-redis 3.x. Version 4 takes an options object, createClient({ socket: { port, host } }), and does not connect until client.connect().
var client = redis.createClient(port, host);
Now, you can perform some action once a connection has been established. Basically, you just need to listen for connect events as shown below.
client.on('connect', function() {
console.log('connected');
});
So, the following snippet goes into app.js:
var redis = require('redis');
var client = redis.createClient();
client.on('connect', function() {
console.log('connected');
});
Run it with node app.js. The Redis server must already be running.