Connection refused on heroku postgresql db via node - node.js

I'm running a postgresql database on heroku, via node. I have my server setup to post to '/submit', which calls a database controller to insert the data into the database. Everything works successfully locally, but when I deploy it to heroku and POST, I get the following error in my heroku logs.
2013-01-21T20:23:43+00:00 heroku[router]: at=info method=POST path=/submit host=[MYDOMAIN].herokuapp.com fwd=[IP] dyno=web.1 queue=0 wait=5ms connect=17ms service=35ms status=200 bytes=2
2013-01-21T20:23:46+00:00 app[web.1]: ^
2013-01-21T20:23:46+00:00 app[web.1]: node.js:201
2013-01-21T20:23:46+00:00 app[web.1]: throw e; // process.nextTick error, or 'error' event on first tick
2013-01-21T20:23:46+00:00 app[web.1]:
2013-01-21T20:23:46+00:00 app[web.1]: at Object.afterConnect [as oncomplete] (net.js:637:18)
2013-01-21T20:23:46+00:00 app[web.1]: Error: connect ECONNREFUSED
2013-01-21T20:23:46+00:00 app[web.1]: at errnoException (net.js:646:11)
2013-01-21T20:23:48+00:00 heroku[web.1]: Process exited with status 1
and a 503 in the application.
Here's the relevant controller code (in coffeescript).
LOCAL_DB = "postgres://localhost:#{DBNAME}"
connect = ->
db = process.env.DATABASE_URL or LOCAL_DB
client = new pg.Client
client.connect()
client
insert = (options) ->
client = connect()
query = client.query "INSERT INTO #{TABLE} VALUES($1, $2, $3, $4);",
[options.uid, options.ls_pref, options.hp_pref, options.date]
query.on "error", onError
query.on "end", -> client.end()
I did promote my database to DATABASE_URL:
$ heroku config | grep DATABASE_URL
> DATABASE_URL: postgres://[URL]
Why is my connection being refused?

It turns out I wasn't passing the database string when creating the client.
# Yes
db = process.env.DATABASE_URL or LOCAL_DB
client = new pg.Client db
client.connect()
# No
db = process.env.DATABASE_URL or LOCAL_DB
client = new pg.Client
client.connect db
The local database was working despite this rather obvious oversight. You've been warned!

Related

Node.js - Heroku server crashes when this code runs

I have a Node.js application that runs perfectly on my localhost.
However, when i uploaded to Heroku, the server crashes once a portion of the code is executed.
The given code is supposed to extract a portion from a URL string that was passed from a form.
This is the code that causes the server to crash:
function getUserDetails(username) {
return new Promise(done => {
var data = [];
https.get(`https://www.instagram.com/${username}/?__a=1`, resp => {
resp.on('data', chunk => data.push(chunk));
resp.on('end', () => {
var json = JSON.parse(data.join(''));
done(json.graphql.user);
});
});
});
}
After Heroku crashedd - and it stil crashes consistently, i checked the logs with heroku logs --tail.
This is the report from the logs:
2019-10-06T10:59:40.782839+00:00 app[web.1]: undefined:1
2019-10-06T10:59:40.782863+00:00 app[web.1]:
2019-10-06T10:59:40.782865+00:00 app[web.1]:
2019-10-06T10:59:40.782867+00:00 app[web.1]:
2019-10-06T10:59:40.782870+00:00 app[web.1]: SyntaxError: Unexpected end of JSON
input
2019-10-06T10:59:40.782872+00:00 app[web.1]: at JSON.parse (<anonymous>)
2019-10-06T10:59:40.782874+00:00 app[web.1]: at IncomingMessage.resp.on (/app/ap
p/routes.js:108:33)
2019-10-06T10:59:40.782876+00:00 app[web.1]: at IncomingMessage.emit (events.js:
203:15)
2019-10-06T10:59:40.782878+00:00 app[web.1]: at endReadableNT (_stream_readable.
js:1145:12)
2019-10-06T10:59:40.782880+00:00 app[web.1]: at process._tickCallback (internal/
process/next_tick.js:63:19)
2019-10-06T10:59:40.869547+00:00 heroku[web.1]: State changed from up to crashed
2019-10-06T10:59:40.847583+00:00 heroku[web.1]: Process exited with status 1
2019-10-06T10:59:40.790256+00:00 heroku[router]: at=error code=H13 desc="Connect
ion closed without response" method=POST path="/MY_PATH" host=MY_HOST.herok
uapp.com request_id=15118004-c799-45bd-a0a9-909cbd3a5e86 fwd="130.43.125.250" dy
no=web.1 connect=1ms service=101ms status=503 bytes=0 protocol=https
Where are you getting the url value from?
Looks like you are reading in some other place from a json input and thats incorrectly formatted or missing a tag or so.
On running your piece of code locally it works as expected.
var URL = require('url').URL;
url = "stackoverflow.com/questions/58256767/node-js-heroku-server-crashes-when this-code-runs";
if(!/^https?:\/\//i.test(url)){
url = "http://" + url;}
let parsed = new URL(url);
let retrieved = parsed.pathname.split('/')[1];
Output :
node v10.16.0
=> 'http://stackoverflow.com/questions/58256767/node-js-heroku-server-crashes-when-this-code-runs'

Bitfinex's Websocket API "hello world" results in ECONNREFUSED error

Bitfinex's Websocket API has the following demo on how to init a connection:
//using the ws library
var WebSocket = require('ws');
var w = new WebSocket("wss://api2.bitfinex.com:3000/ws");
w.onmessage = function(msg) {
console.log(msg.data);
};
Running that example with node.js version v5.9.1 and ws version 1.0.1 results in the following error:
events.js:154
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 178.249.189.15:3000
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1061:14)
What is the cause of that error?
The server is refusing connection...it might be the port (:3000) or the Host name.
Current host for Version 2 is api.bitfinex.com - full url I use is:
wss://api.bitfinex.com/ws/2 as of (7/11/2017)

Following tutorial to connect postgres db to node project - failing at running script to create table

i am following a guide on how to use postgres + node. I have gotten to the point of creating table creation script.
here is the code I am using from the tutorial:
var pg = require('pg');
var connectionString = process.env.DATABASE_URL || 'postgres://postgres:postgres#localhost:5432/todo';
var client = new pg.Client(connectionString);
client.connect();
var query = client.query('CREATE TABLE items(id SERIAL PRIMARY KEY, text VARCHAR(40) not null, complete BOOLEAN)');
query.on('end', function() { client.end(); });
However, when I do the step of node models/database.js, I receive the following error:
ram#ram-windows-xp-ubuntu:~/Development/Web/Projects/node-postgres$ node models/database.js
events.js:85
throw er; // Unhandled 'error' event
^
error: password authentication failed for user "postgres"
at Connection.parseE (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:539:11)
at Connection.parseMessage (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:366:17)
at Socket.<anonymous> (/home/ram/Development/Web/Projects/node-postgres/node_modules/pg/lib/connection.js:105:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
I am new to using the ubuntu and I tried to follow the steps from this guide but confused. Sorry for the bother friends please if you can do the needful I will be humbled.

OpenShift MongoError: auth fails how to resolve?

EDIT ; attaching my app.js , I am using
git add app.js
git commit -m "updated app.js"
git push
command to push code from local machine, and my app.js code is as follows :
/*
*RESTfull server
*/
//defining express middleware
var express=require('express');
//require mongoose, this middleware helps in modeling data for mongodb
var mongoose=require('mongoose');
//require passport, this middleware helps in authentiation
var passport=require('passport');
//require passport, this middleware parsing body
var bodyParser = require('body-parser');
var flash = require('connect-flash');
//define port on which node app is gonna run
//var port = process.env.PORT || 8000;
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080 ;
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1' ;
var app=express();
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
======================================================================
app.listen(server_port,server_ip_address);
console.log('The magic happens on port ' + 'http://'+server_ip_address+':'+server_port);
EDIT :
I commented all mongodb connection code, now my app.js has simple expressjs code, still I can see the same output from the command "rhc tail -a app", Iam not sure why nodejs catridge is trying to connect to mongodb, eventhough there is no code in app.js, is it possible that the log has been generated previously and the same log is being shown ? can I clear log file and test it once? can somebody please help me.
I deployed my nodejs(expressjs) app to the openshift server. I am hitting a mongoError "MongoError: auth fails", I am providing credentials to mongodb server.
Initially when node child process starts it is trying to connect to the
"mongodb://admin:XXXXXX#ip:port" but it should connect to "mongodb://admin:XXXXXX#ip:port/admin" as credentials reside in admin.system.users collection.
I am using mongoose to connect to mongoDB so I changed my mongoose connect to
mongoose.connect(mongodb://admin:XXXXXX#ip:port/admin); But I still see child process is trying to connect to this url "mongodb://admin:XXXXXX#ip:port", but later point of time it connects to the correct collection, and I can see the console ouput of the following code.
mongoose.connection.once('connected', function() {
console.log("Connected to database G")
});
I tested few routes, they are working fine. I want to understand why is it behaving so and can I ignore this error or how can I resolve this issue??
Thanks in advance.
You should be using process.env.OPENSHIFT_MONGODB_DB_URL instead of forming your own url. This environment variable has the following format:
mongodb://admin:LX3eZCP6yxxx#123e4b9a5973ca07ca00002f-appname.rhcloud.com:12345/
Attaching my "rhc tail -a app" commad,
==> app-root/logs/nodejs.log-20150328020443 <==
DEBUG: Starting child process with 'node app.js'
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
The magic happens on port http://127.9.17.129:8080
/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_modules/m
ongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
MongoError: auth fails
at Object.toError (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runt
ime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1194:31
==> app-root/logs/nodejs.log-20150327071155 <==
at EventEmitter.emit (events.js:98:17)
DEBUG: Program node app.js exited with code 8
DEBUG: Starting child process with 'node app.js'
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
mongodb://admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281/
The magic happens on port http://127.9.17.129:8080
/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_modules/m
ongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
MongoError: auth fails
==> app-root/logs/nodejs.log <==
DEBUG: program 'app.js'
DEBUG: --watch '/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/data/.nod
ewatch'
DEBUG: --ignore 'undefined'
DEBUG: --extensions 'node|js|coffee'
DEBUG: --exec 'node'
DEBUG: Starting child process with 'node app.js'
DEBUG: Watching directory '/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/
data/.nodewatch' for changes.
admin:pass#550f3e705973cab149000009-app.rhcloud.com:59281
The magic happens on port http://127.9.17.129:8080
Connected to database G
==> app-root/logs/haproxy.log <==
[WARNING] 087/140540 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 1ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
[WARNING] 088/001408 (417258) : Server express/local-gear is DOWN, reason: Layer
4 connection problem, info: "Connection refused", check duration: 0ms. 0 active
and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 088/001408 (417258) : proxy 'express' has no server available!
[WARNING] 088/002019 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 29ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
[WARNING] 088/110018 (417258) : Server express/local-gear is DOWN, reason: Layer
4 connection problem, info: "Connection refused", check duration: 0ms. 0 active
and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 088/110018 (417258) : proxy 'express' has no server available!
[WARNING] 088/110112 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 1ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
[WARNING] 088/110502 (417258) : Server express/local-gear is DOWN, reason: Layer
4 connection problem, info: "Connection refused", check duration: 0ms. 0 active
and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 088/110502 (417258) : proxy 'express' has no server available!
[WARNING] 088/110556 (417258) : Server express/local-gear is UP, reason: Layer7
check passed, code: 200, info: "HTTP status check returned code <3C>200<3E>", ch
eck duration: 1ms. 1 active and 0 backup servers online. 0 sessions requeued, 0
total in queue.
==> app-root/logs/nodejs.log-20150328074316 <==
The magic happens on port http://127.9.17.129:8080
/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_modules/m
ongoose/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
MongoError: auth fails
at Object.toError (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runt
ime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/utils.js:114:11)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1194:31
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1903:9
at Server.Base._callHandler (/var/lib/openshift/550f3c0ffcf933066f0001b8/app
-root/runtime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connec
tion/base.js:453:41)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
==> app-root/logs/haproxy_ctld.log <==
I, [2015-03-22T18:06:38.808186 #415579] INFO -- : Starting haproxy_ctld
I, [2015-03-27T14:20:21.556898 #15736] INFO -- : Starting haproxy_ctld
I, [2015-03-29T12:18:29.365873 #417278] INFO -- : Starting haproxy_ctld
I, [2015-03-29T12:18:37.485326 #417532] INFO -- : Starting haproxy_ctld
==> app-root/logs/nodejs.log-20150323084556 <==
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/
runtime/repo/app.js:43:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
DEBUG: Program node app.js exited with code 8
==> app-root/logs/nodejs.log-20150328012640 <==
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1194:31
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1903:9
at Server.Base._callHandler (/var/lib/openshift/550f3c0ffcf933066f0001b8/app
-root/runtime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connec
tion/base.js:453:41)
at /var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/runtime/repo/node_mo
dules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
at MongoReply.parseBody (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-roo
t/runtime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/
mongo_reply.js:68:5)
at null.<anonymous> (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/ru
ntime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/ser
ver.js:445:20)
at EventEmitter.emit (events.js:95:17)
at null.<anonymous> (/var/lib/openshift/550f3c0ffcf933066f0001b8/app-root/ru
ntime/repo/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/con
nection_pool.js:207:13)
at EventEmitter.emit (events.js:98:17)
DEBUG: Program node app.js exited with code 8

running Hook.io on a different port

I tried to run hook.io with a different port, which killed the autodiscover features of the clients. But when I try to create the clients with the same port, they get an error.
Sever:
var oHook = hookio.createHook( {
'name' :'dispatch-hook',
'hook-port': 9999,
'hook-host': 'localhost'
} );
oHook.start();
Client:
var oHook = hookio.createHook( {
name :'client-hook',
"hook-port":9999,
"hook-host":'localhost'
});
oHook.connect();
Error:
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:781:11)
at Server._listen2._connectionKey (net.js:922:26)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Why does the client want to start a server?
You shouldn't provide a port for the hook trying to connect to the server hook. The existence of hook-port in options makes that hook a server

Resources