DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. 5-Minute Beginner's Guide: Installing / Testing Redis and Node.js on OS X

5-Minute Beginner's Guide: Installing / Testing Redis and Node.js on OS X

Chad Lung user avatar by
Chad Lung
·
May. 22, 12 · Interview
Like (0)
Save
Tweet
Share
12.60K Views

Join the DZone community and get the full member experience.

Join For Free

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

Reply: OK
Hello World

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
Node.js Redis (company)

Published at DZone with permission of Chad Lung, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Build a Spring Boot GraalVM Image
  • Comparing Map.of() and New HashMap() in Java
  • What Is API-First?
  • Best CI/CD Tools for DevOps: A Review of the Top 10

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: