Not able to connect to mongodb when start node server - node.js

I am trying to run my node app locally on my VM.
When I am running command:
sudo NODE_ENV= XYZ PORT=80 node server/app.js
I'm getting error:
/node_modules/mongo-oplog/node_modules/mongodb/lib/mongodb/mongo_client.js:378
throw err
^ Error: connection closed
at null. (/node_modules/mongo-oplog/node_modules/mongodb/lib/mongodb/connection/server.js:605:24)
at emit (events.js:92:17)
at null. (/node_modules/mongo-oplog/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:155:15)
at emit (events.js:98:17)
at Socket. (//node_modules/mongo-oplog/node_modules/mongodb/lib/mongodb/connection/connection.js:528:12)
at Socket.emit (events.js:95:17)
My mongoDB is running fine and I can connect to local db with robomongo.
Any help would be highly appreciated on this.

Related

MongoError : Server 127.0.0.1:3000 timed out at null

I am getting an error in cmd when i run "node app.js" command.
"facebook-auth\express-authentication" is my path to NodeJS project.
I run my MongoDB and i think it is working fine.
and
When i use "node app.js" command in starting then my "http://127.0.0.1:3000" server work fine but when i close all connections and try to restart mongoDB then is error occur everytime.
I don't know how to solve this problem and this project is running fine in starting on http://127.0.0.1:3000. But don't know what happened know. I closed my MongoDB using Ctrl+C.
After adding this code
// When successfully connected
mongoose.connection.on('connected', function () { console.log('Mongoose default connection open to ' + dbURI); });
// If the connection throws an error
mongoose.connection.on('error',function (err) { console.log('Mongoose default connection error: ' + err); });
to app.js it shows this:
Showing an error and website authentication not work properly(But website is running now). It don't get callback information from localhost or 127.0.0.1. It keeps waiting for response for localhost and callback doesn't complete (stuck there).
I follow this tutorial :Here and project is working fine.
"Is this a conflict between xampp or mongoDB or nodeJS"
App.js file linked Here
Thanks in advance for help.
Waiting for connections on port 27017.
Vs.
Server 127.0.0.1:3000 timed out.
Don't let me think everything is fine.
You either have to change the port, where app.js is connecting to, to Mongo's standard port 27017, or start mongod with --port 3000 to listen on port 3000.
I was facing the same problem and this is what I got each time I run my app.
/home/sarojs/projects/ror/liveProject/bq-ng/brandqast- ng/node_modules/mongoose/node_modules/mongodb/lib/server.js:242
process.nextTick(function() { throw err; })
^
MongoError: server 127.0.0.1:8080 timed out
at null.<anonymous> (/home/sarojs/projects/ror/liveProject/bq-ng/brandqast-ng/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:436:40)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at null.<anonymous> (/home/sarojs/projects/ror/liveProject/bq-ng/brandqast-ng/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:144:10)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/home/sarojs/projects/ror/liveProject/bq-ng/brandqast-ng/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:172:12)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at TCP._onclose (net.js:469:12)
I found that the mongoDB URI that I was setting was 8080 as port no.
I simply changed the port to 27017 as it is the default port. It worked perfectly fine.

Error: Redis connection to 10.130.212.246:6379 failed - connect ETIMEDOUT

I have a sails app. I am running the app in AWS. When i run the code in development mode(sails lift --verbose) it works fine. I am able to access it from the browser bye typing the Ip and port no.(xx.xx.xxx.xx:1337/). But when i run the code in production mode (sails lift --prod --verbose) i am not able to access by ip(xx.xx.xxx.xx) when i try with xx.xx.xxx.xx:1337 it gives me the below error.
Grunt :: Done, without errors.
Unable to parse HTTP body- error occurred:
Error: Redis connection to 10.130.212.246:6379 failed - connect ETIMEDOUT
at RedisClient.flush_and_error (/home/ubuntu/vka/node_modules/sails/node_modules/connect- redis/node_modules/redis/index.js:142:13)
at RedisClient.on_error (/home/ubuntu/vka/node_modules/sails/node_modules/connect-redis/node_modules/redis/index.js:180:10)
at Socket.<anonymous> (/home/ubuntu/vka/node_modules/sails/node_modules/connect-redis/node_modules/redis/index.js:95:14)
at Socket.emit (events.js:95:17)
at net.js:441:14
at process._tickDomainCallback (node.js:492:13) [Error: Redis connection to 10.130.212.246:6379 failed - connect ETIMEDOUT]
Unable to parse HTTP body- error occurred:
Error: Redis connection to 10.130.212.246:6379 failed - connect ETIMEDOUT
at RedisClient.flush_and_error (/home/ubuntu/vka/node_modules/sails/node_modules/connect-redis/node_modules/redis/index.js:142:13)
at RedisClient.on_error (/home/ubuntu/vka/node_modules/sails/node_modules/connect-redis/node_modules/redis/index.js:180:10)
at Socket.<anonymous> (/home/ubuntu/vka/node_modules/sails/node_modules/connect-redis/node_modules/redis/index.js:95:14)
at Socket.emit (events.js:95:17)
at net.js:441:14
at process._tickDomainCallback (node.js:492:13) [Error: Redis connection to 10.130.212.246:6379 failed - connect ETIMEDOUT]
please suggest a possible solution.
Check if you are hardcoding the host in session.js with ip, change it to localhost.
For your question on how to access without port 1337, You need to set up a reverse proxy, say NginX, open up just the port 80 for public access, configure nginx to route the request coming to port 80 to your sails app running on port 1337, use something like pm2 or forever to run the sails app.
Steps to setup NginX as reverse proxy is explained here: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

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.

error when starting the server with grunt or node server

I am trying to use the mean stack as described here : http://mean.io/
after install, when making "grunt", there is an error :
debugger listening on port 5858
4 Jan 01:47:40 - [nodemon] reading ignore list
Express app started on port 3000
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (/mean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (/mean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (/mean/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:415:13)
4 Jan 01:47:40 - [nodemon] app crashed - waiting for file changes before starting...
I tried the troubleshooting as specified on the site : Updating Grunt, bower and cleaning npm and bower caches worked well. but updating npm gives an error too:
npm ERR! error rolling back Error: Refusing to delete: /usr/local/bin/npm not in /usr/local/lib/node_modules/npm
This looks like a conflict between homebrew and npm.
but anyway i have version 1.3.21 installed.
There is the same error when i am trying node server.
do you have an idea on what i can do to make the server work ?
It may be too easy, but seems like your MongoDB server is not actually running. After having installed Mongo (on your Mac, I assume?), you have to open a Terminal window/tab, run mongod and leave it running (or use screen, etc.). You can try to access MongoDB CLI by typing mongo to make sure the server is running.
#desgnl: if you don't want to run mongod command everytime you are running the grunt, you can always use third party services like MongoDB Hosting: Database-as-a-Service by MongoLab https://mongolab.com Hope this helps too!

Unhandled error event in node.js and express app

I was following along to this tutorial on "Authentication Using PassportJS" at this blog
And downloaded the files from his repo at https://github.com/DanialK/PassportJS-Authentication . And I intalled the packages that the terminal instructed me to. Then I attempted to run the app again but got this
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Express server listening on port 3000
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at Server.connect.connectionPool.on.server._serverState (/Users/juangallardo/Desktop/PassportJS-Authentication-master/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74)
at EventEmitter.emit (events.js:115:20)
at connection.on._self._poolState (/Users/juangallardo/Desktop/PassportJS-Authentication-master/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
at EventEmitter.emit (events.js:91:17)
at Socket.errorHandler (/Users/juangallardo/Desktop/PassportJS-Authentication-master/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
at Socket.EventEmitter.emit (events.js:88:17)
at Socket._destroy.self.errorEmitted (net.js:335:14)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
The problem was that I did not have MongoDB running
I found that "issue" discussed on GitHub
Then got the instructions to install and run mongo from this tutorial
brew install mongodb
Then you simply start the mongo server from the terminal with
mongod
Then back in your project just start the app with
node app.js

Resources