Connecting to postgres on Heroku with Node.js - node.js

I try to connect to my postgres database on Heroku by:
var pg = require('pg');
pg.defaults.ssl = true;
var pool = new pg.Pool(process.env.DATABASE_URL);
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
I have confirmed the value of proccess.env.DATABASE_URL it has the right format, and I can successfully connect to it via psql postgres://XXXXXXXX:XXXXXXXXXXXXXXX#XXXXXXXXXXXXX.eu-west-1.compute.amazonaws.com:5432/XXXXXXXX
This is the output, and what confuses me is the 127.0.0.1 reference if this output.
2016-06-27T14:58:52.714154+00:00 app[web.1]: Express server listening on port 18119
2016-06-27T14:59:29.175122+00:00 app[web.1]: error fetching client from pool { Error: connect ECONNREFUSED 127.0.0.1:5432
2016-06-27T14:59:29.175133+00:00 app[web.1]: at Object.exports._errnoException (util.js:949:11)
2016-06-27T14:59:29.175134+00:00 app[web.1]: at exports._exceptionWithHostPort (util.js:972:20)
2016-06-27T14:59:29.175135+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
2016-06-27T14:59:29.175136+00:00 app[web.1]: code: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]: errno: 'ECONNREFUSED',
2016-06-27T14:59:29.175137+00:00 app[web.1]: syscall: 'connect',
2016-06-27T14:59:29.175138+00:00 app[web.1]: address: '127.0.0.1',
2016-06-27T14:59:29.175139+00:00 app[web.1]: port: 5432 }

Try this:
var pg = require('pg').native
pg.connect(process.env.databaseURL, function (err, conn, done) {
...
}

Related

Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete]

I have run the server and getting an error as:
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on Queue instance at:
at RedisClient.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\kue\lib\redis.js:65:13)
at RedisClient.emit (events.js:315:20)
at RedisClient.on_error (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:401:14)
at Socket.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:279:14)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
My Code is as:
const env = require('./environment');
mongoose.connect(`mongodb://localhost/${env.db}`);
const db = mongoose.connection;
db.on('error', console.error.bind(console, "Error connecting to MongoDB"));
db.once('open', function(){
console.log('Connected to Database :: MongoDB');
});
module.exports = db;
I have tried by installing redis and adding it into script but error is same.And also tried in production and development environment but the error is same.
This issue seems to be with redis server. Try running redis-server in the terminal.
Hope this helps!

Redis Overiding remote host in Nodejs

I am connecting to a remote Redis using the following code. The problem connecting because it does not accept the giving IP instead points to localhost.
const redisURL = "redis://20.213.158.211:6379" // random remote IP address
const client = redis.createClient({redisURL});
client.on('connect', function(){
client.select(5);
console.log('Connected to Redis');
});
client.on("error", (err) => console.log("Redis Client Error", err));
client.connect();
It throws the following error
Redis Client Error Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
The problem is it still trying to connect to 127.0.0.1(localhost) instead of the giving IP address above.

ECONNREFUSED , I am trying to connect to my prosgres database running the app with nodemon

node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}

How to connect with mongoDB via SSH

const config = {
username: "username",
password: "password",
host: "0.0.0.0",
port: 22,
dstPort: 27017,
};
tunnel(config, function (error, server) {
if (error) {
console.log("SSH connection error: " + error);
}
console.log("SSH ok");
mongoose.connect(
"mongodb://127.0.0.1:27017/megaparking?retryWrites=true&w=majority'"
);
console.log("Connect ok");
var db = mongoose.connection;
db.on("error", console.error.bind(console, "DB connection error:"));
db.once("open", function () {
console.log("DB connection successful");
});
});
I want to connect with MongoDB installed on server, but I have the issue:
127.0.0.1:27017 is already in use cause
I was opening sever locally and I want connected with MongoDB on dedicated server, which has also 127.0.0.1 host and port 27017. What I should do with that code? Or maybe I should create a MongoDB connection string by this data?
The issue:
Error: listen EADDRINUSE: address already in use 127.0.0.1:27017
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at doListen (net.js:1503:7)
at processTicksAndRejections (internal/process/task_queues.js:81:21)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '127.0.0.1',
port: 27017
}
[nodemon] app crashed - waiting for file changes before starting...

trying to connect to smtp server using nodemailer

Unable to connect to the SMTP server. Is there a better way to connect and check the validity of the SMTP server from the domain?
var net = require('net');
const dns = require('dns')
const SMTPConnection = require("nodemailer/lib/smtp-connection");
dns.resolveMx('gmail.com', function(err, addresses){
if(err) console.log(err);
// just taking the first mail server
h = addresses[0].exchange;
options = {
host:h,
port:25,
connectionTimeout: 3000
};
let connection = new SMTPConnection(options);
connection.connect(()=>{console.log('connected')});
connection.on('error', (err)=>{console.log(err)})
connection.on('end', ()=>{console.log('end')})
})
Error
{ Error: connect EHOSTUNREACH 142.250.11.26:25
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
code: 'ESOCKET',
errno: 'EHOSTUNREACH',
syscall: 'connect',
address: '142.250.11.26',
port: 25,
command: 'CONN' }
end
I get the same error when I try with new net.Socket([options]) from the net module

Resources