MongoDB fails to respond on localhost but works for another node app - node.js

I'm working on a new node.js app on an old server I use for dev stuff.
Both apps have the mongo URL specified:
var mongoUri = 'mongodb://localhost/node';
but on startup, the old node.js app works fine, but the new one doesn't. The new one returns the following for node server.js
Error: unable to connect to database at mongodb://localhost/node
at NativeConnection.<anonymous> (/var/www/server/server.js:15:9)
at emitOne (events.js:115:13)
at NativeConnection.emit (events.js:210:7)
at /var/www/server/node_modules/mongoose/lib/connection.js:336:19
at args.push (/var/www/server/node_modules/mongodb/lib/utils.js:403:25)
at /var/www/server/node_modules/mongodb/lib/mongo_client.js:254:21
at connectCallback (/var/www/server/node_modules/mongodb/lib/mongo_client.js:933:5)
at /var/www/server/node_modules/mongodb/lib/mongo_client.js:782:11
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Any thoughts or suggestions would be really appreciated. I've tried everything short of reinstalling mongo at this point :(

Related

Node.js Error: Unexpected server response: 301 - Running a Node.js Websocket server on Namecheap Webhost

I'm trying to setup a Node.js Websocket server over at my webhost (Namecheap). However, I am getting this error:
events.js:292
throw er; // Unhandled 'error' event
^
Error: Unexpected server response: 301
at ClientRequest.<anonymous> (D:\****\node_modules\ws\lib\websocket.js:576:7)
at ClientRequest.emit (events.js:315:20)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:596:27)
at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)
at TLSSocket.socketOnData (_http_client.js:469:22)
at TLSSocket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at TLSSocket.Readable.push (_stream_readable.js:212:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on WebSocket instance at:
at abortHandshake (D:\****\node_modules\ws\lib\websocket.js:694:15)
at ClientRequest.<anonymous> (D:\****\node_modules\ws\lib\websocket.js:576:7)
[... lines matching original stack trace ...]
at TLSSocket.Readable.push (_stream_readable.js:212:10)
The code for the client and server came from here:
https://github.com/websockets/ws#Simple server
https://github.com/websockets/ws#sending-and-receiving-text-data
with ws://www.host.com/path changed to ws://<mydomainname>/<dir1>/<dir2>
Both client and server code runs properly when run locally. Http related code runs properly when used on my webhost. I am trying to get the client code to run locally and connect via websockets to my server hosted at Namecheap.
I suspect something related to the server is preventing connection. I'd ask support but I doubt if they can resolve this. Maybe there's a locked feature or something? Is there something else I could have missed?
Websockets cannot work on Namecheap's hosted servers. Their incoming ports are blocked and cannot be opened for security reasons per their customer's support.
If anyone is looking for a solution just in case, try out a Heroku free account.

MongoDB error while installing Rocket.chat in CentOS 8 server with Apache

I am following this tutorial to install a Rocket.chat server in CentOS:
https://hostpresto.com/community/tutorials/how-to-install-rocket-chat-server-on-centos-7/
I am new to mongodb and node.js so I am not very familiar with how all this works
At the moment I am looking for something simple that just works
When I reach the step where it says to execute
node main.js
I get the following error:
[username#localhost Rocket.chat]$ node main.js
/opt/Rocket.chat/programs/server/node_modules/fibers/future.js:313
throw(ex);
^
MongoError: not master and slaveOk=false
at Connection.<anonymous> (/opt/Rocket.chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/core/connection/pool.js:450:61)
at Connection.emit (events.js:310:20)
at processMessage (/opt/Rocket.chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/core/connection/connection.js:384:10)
at Socket.<anonymous> (/opt/Rocket.chat/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/core/connection/connection.js:553:15)
at Socket.emit (events.js:310:20)
at addChunk (_stream_readable.js:286:12)
at readableAddChunk (_stream_readable.js:268:9)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
ok: 0,
errmsg: 'not master and slaveOk=false',
code: 13435,
codeName: 'NotMasterNoSlaveOk',
[Symbol(mongoErrorContextSymbol)]: {}
}
The tutorial says I should be able to see the Rocket.chat web interface if I go to https://yourdomain.com:3000 but I get an unable to connect page, which I suppose is due to the error above
The tutorial also says node main.js is to check if MongoDB is running or not. I can see that MongoDb service is running by executing
systemctl status mongod
so this error seems to be caused by something else.
How do I find out what is causing this error? Or are there any mistakes in this tutorial? I have found other tutorials regarding installing Rocket.chat in CentOS but most of them use nginx as hte web server while I need to use Apache.

ioredis Unhandled error event: Error: connect ETIMEDOUT

Has anyone encountered below error while connecting to standalone redis server using node js ioredis package?
Below is the error stack trace:
2018-08-16T10:52:18.351869060Z [ioredis] Unhandled error event: Error: connect ETIMEDOUT
2018-08-16T10:52:07.449457296Z at Timer.listOnTimeout (timers.js:207:5)
2018-08-16T10:52:07.449448499Z at tryOnTimeout (timers.js:237:5)
2018-08-16T10:52:07.449439722Z at ontimeout (timers.js:365:14)
2018-08-16T10:52:07.449430834Z at Socket._onTimeout (net.js:339:8)
2018-08-16T10:52:07.449421915Z at Socket.emit (events.js:185:7)
2018-08-16T10:52:07.449413002Z at emitNone (events.js:86:13)
2018-08-16T10:52:07.449403458Z at Socket.g (events.js:291:16)
This is occurring for instantiating only Standalone Redis object in node js. Below is the code I am using,
var publisher = new redis(redisPort, redisHost);
any solution would be highly appreciated.
You can probably try increasing the timeout limit since ioredis has a default timeout value.
Normally we would have it set as,
new Redis({
connectTimeout: 10000
})
In your case, since you have,
var publisher = new redis(redisPort, redisHost);
You will have to edit your code to pass the connectTimeout parameter to be passed accordingly.
Hope this helps.
It's a bit late but can be helpful in the future for somebody else.
const redis = new Redis({
port: <your_redis_port>,
host: <your_redis_hostname>,
connectTimeout: 10000
});
I am using Redis client maintained by Heroku and for some strange reason the Redis credentials have been changed by Heroku, I have been facing the same issue until I double-check the old credentials and the one on Heroku and I realised they were no longer the same then copied the new one from Heroku and pasted it in my .env file and everything is working as expected now!!!

Heroku fails to load page. MongoError: Authentication failed

I been trying to upload my app to Heroku But I keep getting this MongoError: authentication fail error. I have both username and password correctly. Can someone help me out. I tried installing different version But no use.
running mongod --version
db version v3.4.4
git version: 888390515874a9debd1b6c5d36559ca86b44babd
In my package.json I have
"dependencies": {
"mongoose": "^4.4.12",
"mongoose-unique-validator": "^1.0.2"
},
app.js
var mongoose = require('mongoose');
mongoose.connect('<username>:<password>#ds129023.mlab.com:29023/ng2');
heroku logs:-
throw er; // Unhandled 'error' event
MongoError: Authentication failed.
at Function.MongoError.create (/app/node_modules/mongodb-
core/lib/error.js:31:11)
at /app/node_modules/mongodb-core/lib/connection/pool.js:497:72
^
at authenticateStragglers (/app/node_modules/mongodb-
core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/app/node_modules/mongodb-
core/lib/connection/pool.js:477:5)
at Socket.<anonymous> (/app/node_modules/mongodb-
core/lib/connection/connection.js:321:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
Thanks
You are not providing protocol name here
var mongoose = require('mongoose');
mongoose.connect('mongodb://<username>:<password>#ds129023.mlab.com:29023/ng2');
I added mongodb before username. Try with this, it should work.
I had the same problem.
Make sure that you've created a user for your database
For case with "admin" user, it could be:
mongoose.connect('mongodb:admin:#ds129023.mlab.com:29023/ng2');
I have faced the same problem in my case I have not added config vars in Heroku app.
Go to Heroku website > Your App > Settings > Reveal Config Vars and add your environment variables there.
As You Can see in the following image.

Mongoose Can't Connect Without Internet

I have my MongoDB server running on localhost:27017, and while I can usually run my Node.js app fine, when I disconnect from the internet Mongoose throws the error
Error: failed to connect to [localhost:27017]
Note that I can still connect to the MongoDB server from the Mongo shell client. Also, if I start up my app first and then lose internet connection, my app can access the database fine offline. So why can't it start up without internet?
EDIT: here is the error in full
events.js:85
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (<My App>\node_modules\mongoose\
node_modules\mongodb\lib\mongodb\connection\server.js:555:74)
at emit (events.js:118:17)
at null.<anonymous> (<My App>\node_modules\mongoose\
node_modules\mongodb\lib\mongodb\connection\connection_pool.js:156:15)
at emit (events.js:110:17)
at Socket.<anonymous> (<My App>\node_modules\mongoos
e\node_modules\mongodb\lib\mongodb\connection\connection.js:534:10)
at Socket.emit (events.js:107:17)
at net.js:923:16
at process._tickCallback (node.js:355:11)
[nodemon] app crashed - waiting for file changes before starting...
Edit: wording
Use 127.0.0.1 instead of localhost. By turning off your wifi interface the OS is no longer able to resolve localhost.
Consider node-offline-localhost.
Add the following before the breaking code:
require('node-offline-localhost').always();
And it just works (hopefully), at least until RFC 3493 gets fixed.
Full disclosure: I authored this package to streamline https in my dev environment when offline.

Resources