Aditya Gupta
Aditya Gupta

Reputation: 79

use redis as a database using nodeJS

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 

> [email protected] 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)

Upvotes: 0

Views: 3093

Answers (3)

Fahad
Fahad

Reputation: 2053

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

Upvotes: 0

Scadoodles
Scadoodles

Reputation: 151

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

Upvotes: 2

mikefrey
mikefrey

Reputation: 4691

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();

Upvotes: 1

Related Questions