5-Minute Beginner's Guide: Installing / Testing Redis and Node.js on OS X
Join the DZone community and get the full member experience.
Join For FreeRedis is an open source, advanced key-value store. Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Today I’m going to show you how quickly you can get Redis and Node.js running on OS X. In addition, I’ll show you two quick demos you can then try out with Node.js.
Note: I’m using OS X version 10.7.3 and I have the latest XCode installed (available on the App Store). Much of this information is available at the respective websites, I just wanted to put it all in one place for beginners.
Step one, go grab the OS X package from the Nodejs website and download and install it. The package is pre-built and is very simple to install.
Step Two: Once the above is done go to the Redis website and download the latest Redis package. Once the tar file was downloaded I double clicked it to extract it, you can also issue the follow command line from the terminal:
$ tar xzf redis-2.x.x.tar.gz
From the terminal go into the new folder that was created when you extracted Redis:
$ cd redis-2.x.x
Still within the Redis folder simply run the make command now:
$ make
Test everything out:
$ make test
Time to start Redis:
$ src/redis-server
Let’s start with a very simple program. The program will simply set a value and then fetch it back.
First, create a new folder and inside that folder create a new text file called app.js. Inside the app.js file add the following code:
var redis = require("redis"), client = redis.createClient(); client.on("error", function (err) { console.log("Error " + err); }); // Set a value client.set("string key", "Hello World", redis.print); // Get the value back client.get("string key", function (err, reply) { console.log(reply.toString()); }); // Clean quit (waits for client to finish) client.quit();
Now, let’s install node_redis locally via NPM:
Go back into the terminal and navigate the folder where you placed the app.js file and enter this command from within that folder:
$ npm install redis
Note: You can also install a non-blocking C library version like this:
$ npm install hiredis redis
Run the Node.js program like so:
$ node app.js
Output will look similar to this:
Lets build a more complex program now. Modify the app.js file and replace the previous code with the code below (taken from the node_redis project on Github):
var redis = require("redis"), client = redis.createClient(); client.on("error", function (err) { console.log("Error " + err); }); client.set("string key", "string val", redis.print); client.hset("hash key", "hashtest 1", "some value", redis.print); client.hset(["hash key", "hashtest 2", "some other value"], redis.print); client.hkeys("hash key", function (err, replies) { console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); client.quit(); });
Run the Node.js program now like so:
$ node app.js
Output will look similar to this:
Reply: OK Reply: 1 Reply: 1 2 replies: 0: hashtest 1 1: hashtest 2 |
In the terminal and the Redis folder you can safely shutdown Redis with the following command:
$ src/redis-cli shutdown
Published at DZone with permission of Chad Lung, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments