MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS with Redis

Getting Started


node_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');
client = redis.createClient(); //creates a new client

By default, redis.createClient() will use 127.0.0.1 and 6379 as the hostname and port respectively. If you have a different host/port you can supply them as following:

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');
});

Now, type node app in the terminal to run the app. Make sure your Redis server is up and running before running this snippet.

Conclusion

In this page (written and validated by ) you learned about NodeJS with Redis . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Using Browserfiy to resolve required error with browsers.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.