NodeJS connect-redis with nodejitsu URI - node.js

I create a Redis db on nodejitsu and got a full URI such as:
nodejitsu:nodejitsudb11158161232.redis.irstack.com:f327c9sj29sj26e80b8e975fbebb4#nodejitsudb3058169sj32.redis.irstack.com
Connect-redis wants a host, port, etc... Anyone know of an easy way I can just pass it this string? Didn't seem to work when setting the host to that - mongodb seems to work fine with this schema.

Use url.parse http://nodejs.org/docs/latest/api/url.html to parse that url into its parts such as host, port, etc....

So diving in the source it looks like it's unable to parse a dsn string. url.parse doesn't return the necessary info so it has to be broken down further.
The full dsn has the redis:// protocol as well - it must be there for this to work. Because my local redis dsn string doesn't have a password/user there is a check for that in case it's missing:
var redisUrl = 'redis://nodejitsu:nodejitsudb3051531232.redis.irstack.com:fd470c971946e8der75fbebb4#nodejitsudb305813frt232.redis.irstack.com:6379';
var redis = url.parse(redisUrl);
app.use(express.session({
secret: settings.sessionSecret,
store: new RedisStore({host: redis.hostname, port: redis.port, pass: redis.auth ? redis.auth.substring(redis.auth.indexOf(':') + 1): null})
}));

Related

Connect AWS redis to node using node-redis

I am using node-redis and having a hard time connecting to external redis instance. I tried with redis-cli and it worked. However with node I am not able to figure out how to properly give the url and port.
With Redis-cli-
redis-cli -h mydomain.something.something.cache.amazonaws.com -p 6379
However with nodejs
Below didn't work
var client = redis.createClient('redis://mydomain.something.something.cache.amazonaws.com:6379'),
neither
var client = redis.createClient({host:'redis://mydomain.something.something.cache.amazonaws.com', port: 6379});
How do I configure it. Please help.
Following should work with node.js -
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
Also, make sure that your security group on AWS allows you to access the database.
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
With the above code, it was always trying to connect with localhost,
Below code worked for me.
var client = require('redis').createClient(
{
url: `redis://${elasticCacheConnectionString}`,
}
);
Please note, i have appended redis:// as communication protocol before actual connection string.
FYI: I am using redis#4.1.0 version.

NodeJS encrypted connection string to Postgres

I am learning NodeJS by building a JWT server. Basically I want to authorize users against credentials in a PostgreSQL database. I am considering node-postgres, passport, pg to connect with PostgreSQL but I have not found anyway to store my connection values encrypted. Ideally I would store them in a properties file so I can change them per environment.
Most examples I see do something like:
var pg = require('pg');
var conString = "postgres://YourUserName:YourPassword#localhost:5432/YourDatabase";
Can someone help show me how to encrypt and use my credentials so I don't have to hard code the plain values in my source?
There seem to exist npm packages for this already. See https://www.npmjs.com/package/secure-conf. Seems to fulfill your needs.
Please note, that you should also secure your Connection to the DB using SSL. See SSL for PostgreSQL connection nodejs for a Solution.
This should help.
if you use sequelize to connect postgres
const sequelize = new Sequelize("DB", usrname, password, {
host: "/var/run/postgresql",
dialect: "postgres",
});
NB: get the host string from your pgsl db might be different //

Using Redis for session store doesn't seem to work

adapting an existing app to Express 4.4.x
Trying to implement redis session store, using the following code:
var express = require('express');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var redis = require('redis').createClient();
app.use(session({
store: new RedisStore({
host: '1.1.1.1',
port: 1234,
prefix: 'yourprefix:',
client: redis
}),
secret: '.......',
resave: true,
saveUninitialized: true
}));
However, when I run this there is no key stored in redis and I cannot call req.session. I'm sure it's something really simple I've missed or included something in the code which is restricting it. Could also be redis settings?
Thanks
Ok, I just got lost in the documentation. Difficult to keep track of express-session, redis, connect-redis and entirely remember the difference between everything.
The simple solution was:
var redis = require('redis').createClient(port, host, auth);
Now it works. I believe I was able to use .createClient(); when the redis server was hosted locally, however now we've moved to a dedicated redis server - these attributes are required.
Thanks, hope this helps somebody else!

connecting to RedisToGo on Heroku thru Nodejs [duplicate]

I'm using Redis To Go in combination with the https://github.com/mranney/node_redis library. Redis gives me a url that looks like redis://me:978287c0b670694673d045f08b2e0371#icefish.redistogo.com:9393 but I don't know how to use it as createClient() only takes the host and the port.
I believe that the scheme for the URL you have is:
redis://username:password#host:port.
I don't believe username is used. node_redis provides two methods that you'll use to log in: createClient and auth. There are details in the readme, but for reference here is the relevant portion:
redis.createClient(port, host, options)
Create a new client connection. port defaults to 6379 and host
defaults to 127.0.0.1. If you have redis-server running on the
same computer as node, then the defaults for port and host are
probably fine. options in an object with the following possible
properties:
parser: which Redis protocol reply parser to use. Defaults to
hiredis if that module is installed. This may also be set to
javascript.
return_buffers: defaults to false. If set to true, then bulk
data replies will be returned as node Buffer objects instead of
JavaScript Strings.
createClient() returns a RedisClient object that is named client
in all of the examples here.
client.auth(password, callback)
When connecting to Redis servers that require authentication, the
AUTH command must be sent as the first command after connecting.
This can be tricky to coordinate with reconnections, the ready check,
etc. To make this easier, client.auth() stashes password and will
send it after each connection, including reconnections. callback is
invoked only once, after the response to the very first AUTH command
sent.
I also had to add the parameter no_ready_check: true to the call to redis.createClient().
client = redis.createClient(settings.redis.port,
settings.redis.host,
{no_ready_check: true});
if (settings.redis.password) {
client.auth(settings.redis.password, function() {
console.log('Redis client connected');
});
}

Connecting to RedisToGo through Node.JS

I'm using Redis To Go in combination with the https://github.com/mranney/node_redis library. Redis gives me a url that looks like redis://me:978287c0b670694673d045f08b2e0371#icefish.redistogo.com:9393 but I don't know how to use it as createClient() only takes the host and the port.
I believe that the scheme for the URL you have is:
redis://username:password#host:port.
I don't believe username is used. node_redis provides two methods that you'll use to log in: createClient and auth. There are details in the readme, but for reference here is the relevant portion:
redis.createClient(port, host, options)
Create a new client connection. port defaults to 6379 and host
defaults to 127.0.0.1. If you have redis-server running on the
same computer as node, then the defaults for port and host are
probably fine. options in an object with the following possible
properties:
parser: which Redis protocol reply parser to use. Defaults to
hiredis if that module is installed. This may also be set to
javascript.
return_buffers: defaults to false. If set to true, then bulk
data replies will be returned as node Buffer objects instead of
JavaScript Strings.
createClient() returns a RedisClient object that is named client
in all of the examples here.
client.auth(password, callback)
When connecting to Redis servers that require authentication, the
AUTH command must be sent as the first command after connecting.
This can be tricky to coordinate with reconnections, the ready check,
etc. To make this easier, client.auth() stashes password and will
send it after each connection, including reconnections. callback is
invoked only once, after the response to the very first AUTH command
sent.
I also had to add the parameter no_ready_check: true to the call to redis.createClient().
client = redis.createClient(settings.redis.port,
settings.redis.host,
{no_ready_check: true});
if (settings.redis.password) {
client.auth(settings.redis.password, function() {
console.log('Redis client connected');
});
}

Resources