Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js and Redis Auth

Tags:

node.js

redis

I dont get the redis node.js documentation for using redis auth.

per the example:

var redis = require("redis"),
client = redis.createClient();

// This command is magical. Client stashes the password and will issue on every connect.
client.auth("somepass");

In my code I have the following:

var redis = require("redis");
r = redis.createClient(6379,'xxx.xxx.xxx.xxx');
r.auth("yyyyyyyy");

app.get('/', function(req, res){
r.set("foo", 'bar');
res.writeHead(200, {'Content-Type': 'image/gif'});
res.end('Hello');
 });

Here is the error I get:

    Express server listening on port 8070 in development mode

/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:468
                throw callback_err;
                      ^
Error: Auth error: Error: ERR Client sent AUTH, but no password is set
    at Command.callback (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:163:43)
    at RedisClient.return_error (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:464:25)
    at HiredisReplyParser.<anonymous> (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:253:14)
    at HiredisReplyParser.emit (events.js:67:17)
    at HiredisReplyParser.execute (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/lib/parser/hiredis.js:41:18)
    at RedisClient.on_data (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:440:27)
    at Socket.<anonymous> (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/redis/index.js:70:14)
    at Socket.emit (events.js:67:17)
    at TCP.onread (net.js:367:14)
[7]+  Killed                  node app.js 8070

So, what is the proper way to auth?

like image 572
Tampa Avatar asked May 19 '12 16:05

Tampa


People also ask

How does Redis integrate with node js?

Create new session. js file in the root directory with the following content: const express = require('express'); const session = require('express-session'); const redis = require('redis'); const client = redis. createClient(); const redisStore = require('connect-redis')(session); const app = express(); app.

How do you Auth Redis?

When the authorization layer is enabled, Redis will refuse any query by unauthenticated clients. A client can authenticate itself by sending the AUTH command followed by the password. The password is set by the system administrator in clear text inside the redis. conf file.

How use Redis AUTH command?

Redis AUTH command is used to authenticate to the server with the given password. If the password matches the password in the configuration file, the server replies with the OK status code and starts accepting commands. Otherwise, an error is returned and the clients needs to try a new password.

Why we use Redis in node JS?

Redis, an in-memory database that stores data in the server memory, is a popular tool to cache data. You can connect to Redis in Node. js using the node-redis module, which gives you methods to retrieve and store data in Redis.


1 Answers

The following code creates a connection to Redis using node package redis.

const redis = require('redis');

const client = redis.createClient({
    host: '<hostname>',
    port: <port>,
    password: '<password>'
});
like image 68
Manoj Rana Avatar answered Sep 30 '22 07:09

Manoj Rana