OpenShift MongoError: auth fails how to resolve? - node.js

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

Related

Despite app.listen, express app does not bind to port

Exactly what it sounds like.
I use ExpressJS for my Node app, which is hosted on Heroku.
Despite using app.listen, it consistently is getting / causing heroku R10 errors, which are caused by a web app not binding to process.env.PORT in time.
The relevant code:
const app = express();
var isRoot = (process.getuid && (process.getuid() === 0));
var port;
if (isRoot) {
port = 80;
} else {
port = process.env.PORT | 8000;
}
const server = app.listen(port, onStartup);
function onStartup() {
console.log("Started webserver on port "+port);
}
Now the odd thing is, I'm getting the "Started webserver on port [foo]" message, it's just not binding to the port.
Logs:
2020-03-30T19:50:39.434302+00:00 app[web.1]: > foo-bar#1.0.0 start /app
2020-03-30T19:50:39.434303+00:00 app[web.1]: > node scrape2.js
2020-03-30T19:50:39.434303+00:00 app[web.1]:
2020-03-30T19:50:39.829882+00:00 app[web.1]: Verbose mode OFF
2020-03-30T19:50:39.830782+00:00 app[web.1]: Started webserver on port 8052
2020-03-30T19:51:37.415192+00:00 heroku[web.1]: State changed from starting to crashed
2020-03-30T19:51:37.293060+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-03-30T19:51:37.293142+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-03-30T19:51:37.391762+00:00 heroku[web.1]: Process exited with status 137
Help!
I did a stupid and accidentally used the bitwise OR operator, which caused it to bind to a different port than process.env.PORT. Changed it from | to || and it works fine now.

Socket.io EADDRNOTAVAIL error

I oppened port of 120 on my firewall and i open ufw port on my server (Ubuntu 16.04)
But when run this code ;
var app = require('express')();
var http = require( "http" ).createServer( app );
var io = require( "socket.io" )( http );
http.listen(120, "xxxx.xxx.xx");
io.on('connection',function(socket){
console.log("A user is connected");
});
I get this error ;
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL xxxx.xxxxxx:120
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1224:19)
at listen (net.js:1273:10)
at net.js:1382:9
at nextTickCallbackWith3Args (node.js:452:9)
at process._tickCallback (node.js:358:17)
at Function.Module.runMain (module.js:444:11)
at startup (node.js:136:18)
at node.js:966:3
It may be that port 120 is already being used by something else.
You can use netstat to see what is listening on that port:
sudo netstat -plnt | grep ':120'
Another thing to mention is that low ports are sometimes reserved or blocked - you may want to just try a higher port, '1337' is always good for NodeJS :-)

Node js 0.10.7: cluster support for udp dgram?

I'm trying to run following node js application as mentioned https://github.com/joyent/node/issues/2194
var util = require("util"),
dgram = require("dgram"),
cluster = require('cluster');
var udp = dgram.createSocket("udp4");
var port = 1190;
if (cluster.isMaster) {
for (i = 0; i < 2; i++) {
cluster.fork();
}
} else {
util.log("starting udp server on port " + port);
udp.on("error", function (error) {
util.log("failed to bind to UDP port - " + error)
});
udp.bind(port);
}
The app exits immediately with the following output:
23 May 23:22:13 - starting udp server on port 1190
23 May 23:22:13 - starting udp server on port 1190
events.js:72
throw er; // Unhandled 'error' event
^
Error: write ENOTSUP - cannot write to IPC channel.
at errnoException (child_process.js:980:11)
at ChildProcess.target.send (child_process.js:455:16)
at Worker.send (cluster.js:401:21)
at sendInternalMessage (cluster.js:394:10)
at handleResponse (cluster.js:177:5)
at respond (cluster.js:192:5)
at Object.messageHandler.queryServer (cluster.js:242:5)
at handleMessage (cluster.js:197:32)
at ChildProcess.EventEmitter.emit (events.js:117:20)
at handleMessage (child_process.js:318:10)
Does anyone know what is going on? When running this without cluster, everything is fine.
It seems that cluster does not support udp?
Some specs:
Window 7 x64
node js 0.10.7
It says in the link your provided that support for UDP clustering was added in v0.11.14. It is likely that you simply need to update your version of node.js

Connection refused on heroku postgresql db via node

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!

Node.js, dgram.setBroadcast(flag) fails due to "EBADF"

I'm using Node.js 0.6.9, and am trying to send a datagram broadcast package. Code:
var sys = require('util');
var net = require('net');
var dgram = require('dgram');
var message = new Buffer('message');
var client = dgram.createSocket("udp4");
client.setBroadcast(true);
client.send(message, 0, message.length, 8282, "192.168.1.255", function(err, bytes) {
client.close();
});
Running the code:
$ node test.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: setBroadcast EBADF
at errnoException (dgram.js:352:11)
at Socket.setBroadcast (dgram.js:227:11)
at Object.<anonymous> (/home/letharion/tmp/collision/hello.js:25:8)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:32)
at Function._load (module.js:310:12)
at Array.0 (module.js:482:10)
at EventEmitter._tickCallback (node.js:192:41)
Some googling reveals that "EBADF" means "The socket argument is not a valid file descriptor". But I don't understand enough about the problem for that to be helpful.
First of all, you seem to have trouble understanding the format of the stacktrace, so let's clarify it before we go to the actual error that is thrown here.
Format of a node.js Stacktrace
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
This part is just the location where the internal NodeJS logic choked up and put out the error below:
The actual error stacktrace follows, it shows the deepest location in the callstack first, so going down in the stack trace, brings you up in the call hierachy, eventually leading you to the point in your code where everything began.
Error: setBroadcast EBADF
at errnoException (dgram.js:352:11)
at Socket.setBroadcast (dgram.js:227:11)
at Object.<anonymous> (/home/letharion/tmp/collision/hello.js:25:8)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:32)
at Function._load (module.js:310:12)
at Array.0 (module.js:482:10)
at EventEmitter._tickCallback (node.js:192:41)
First it fails in dgram.js on line 352, dgram.js is a internal node.js module abstracting the "low level" code. Line 352 is in a function containing generic logic for throwing errors.
It was called at dgram.js in line 227, after a failed if check which wraps the call to the wrapped native UDP sockets setBroadcast method.
Going up one more layer, we end up at your hello.js file on line 25 with the client.setBroadcast(true); call.
The rest is more node.js code resulting from the initial load of the hello.js file.
The actual Error
The error thrown by the native code which node.js wraps here is EBADF looking this up in conjunction with UDP gives us:
EBADF
The socket argument is not a valid file descriptor.
By going further down into the node.js rabbit hole, we end up in the udp wrapper, which wraps the uv wrapper for the actual C implementation, in the uv wrapper we find:
/*
* Set broadcast on or off
*
* Arguments:
* handle UDP handle. Should have been initialized with
* `uv_udp_init`.
* on 1 for on, 0 for off
*
* Returns:
* 0 on success, -1 on error.
*/
Leading us to the conclusion that your socket has not been initialized yet.
In the end, binding the socket via client.bind(8000) fixed the missing initialization and made the program run.
The method setBroadcast should be called on 'listening' event or passed as callback in bind method:
var socket = dgram.createSocket('udp4');
socket.on('listening', function(){
socket.setBroadcast(true);
});
socket.bind(8000);
OR:
var socket = dgram.createSocket('udp4');
socket.bind(8000, undefined, function() {
socket.setBroadcast(true);
});
It seems like the file descriptor is created only on bind or on send, and it's required before setBroadcast. You can call client.bind() with no parameter to bind to a random port before setting broadcast. Don't worry about using a random port, since it's done "lazily" when using client.send anyway.
var sys = require('util');
var net = require('net');
var dgram = require('dgram');
var message = new Buffer('message');
var client = dgram.createSocket("udp4");
client.bind();
client.setBroadcast(true);
client.send(message, 0, message.length, 8282, "192.168.1.255", function(err, bytes) {
client.close();
});

Resources