0

I use 'redis' module in my app. But it's throw error. My code is following -

//app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var redis = require('redis');

var client = redis.createClient('localhost', 3000); 
client.on('connect', function() {
    console.log("connected");
});

Here is the error:

Adityas-MacBook-Air:node_elastic_redis adityagupta$ npm start 

> node_elastic_redis@0.0.0 start /Users/adityagupta/Desktop/node_elastic_redis 
> node ./bin/www events.js:154 throw er; // Unhandled 'error' event ^ 

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 
    at Object.exports._errnoException (util.js:856:11) 
    at exports._exceptionWithHostPort (util.js:879:20) 
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)
mikefrey
  • 4,653
  • 3
  • 29
  • 40
Aditya Gupta
  • 79
  • 1
  • 3
  • 8
  • Redis usually installs itself on port 6379, why are you using 3000? – Vsevolod Goloviznin Mar 08 '16 at 19:22
  • Adityas-MacBook-Air:node_elastic_redis adityagupta$ npm start > node_elastic_redis@0.0.0 start /Users/adityagupta/Desktop/node_elastic_redis > node ./bin/www events.js:154 throw er; // Unhandled 'error' event ^ Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 at Object.exports._errnoException (util.js:856:11) at exports._exceptionWithHostPort (util.js:879:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14) – Aditya Gupta Mar 08 '16 at 19:27
  • my node application using 3000. – Aditya Gupta Mar 08 '16 at 19:30

3 Answers3

2

The port should be the first argument according to documentation like so:

redis.createClient(port[, host][, options])

Or in your case:

var client = redis.createClient(6379, 'localhost');
Scadoodles
  • 151
  • 6
1

Based on the error message, either redis is not running, or not running on the port specified.

Try using the default redis port of 6379. If you are running redis on the same machine the node app is running on, you may not have to specify the host and port:

var client = redis.createClient('localhost', 6379);

or

var client = redis.createClient();
mikefrey
  • 4,653
  • 3
  • 29
  • 40
  • By using redis.createClient('localhost', 6379); It's generate - /Users/adityagupta/Desktop/node_elastic_redis/node_modules/redis/index.js:1303 options.path = port_arg; – Aditya Gupta Mar 08 '16 at 19:55
0

You only installed a driver without the actual server , For redis to work ... you have to install redis server on your local machine.

Head to Redis Official Site to get the latest version of redis , if you are using windows by any chance , Have a look at these

Community
  • 1
  • 1
Fahad
  • 1,943
  • 22
  • 27