Failing to connect to MongoDB on 27017 in NodeJs - node.js

I have updated NodeJs to V 0.12.7 and now when i try to start express server it gets started but its not getting connect to mongodb. Here's the stack trace.
F:\Node Restful API with Express4\server
> nodemon
12 Sep 10:50:51 - [nodemon] v1.3.7
12 Sep 10:50:51 - [nodemon] to restart at any time, enter `rs`
12 Sep 10:50:51 - [nodemon] watching: *.*
12 Sep 10:50:51 - [nodemon] starting `node server.js`
Magic happens on port 4040
events.js:85
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (F:\Node Restful API with Express4\server\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:540:78)
at emit (events.js:118:17)
at null.<anonymous> (F:\Node Restful API with Express4\server\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection_pool.js:140:19)
at emit (events.js:110:17)
at Socket.<anonymous> (F:\Node Restful API with Express4\server\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:478:14)
at Socket.emit (events.js:107:17)
at net.js:950:16
at process._tickCallback (node.js:355:11)
12 Sep 10:50:52 - [nodemon] app crashed - waiting for file changes before starting...

On Windows the localhost will not be available if the computer is not connected to the internet. Hence we need to write mongodb://127.0.0.1:27017 to connect to MongoDB.

Related

Run NodeJS server from AWS EC2

EC2 is properly deployed with my NodeJS server but I can't figure out how to keep it running without an ssh connection to the linux machine and running npm start
For Example:
[ec2-user#ip-172-31-89-105 me_server]$ npm start
> me-server#1.0.0 start
> nodemon index.js
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Started at 8080
Database Connected
I can run this command from an ssh tunnel from my local machine but I don't want my local machine to be in control of the server connection, how can I tell EC2 to run npm start on its own and keep it running indefinitely?

Getting an error while running "nodemon app" comannd

i've downloaded express package and nodemon, I created a server and rendered all of my ejs files . while running "nodemon app" command I'm getting this error, what specifically the problem is? :
PS C:\Users\user\Desktop\my port> nodemon app
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
events.js:291
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1316:16)
at listenInCluster (net.js:1364:12)
at Server.listen (net.js:1450:7)
at Function.listen (C:\Users\user\Desktop\my port\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\user\Desktop\my port\app.js:16:5)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1343:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
Your system has something already running on port 3000.
you can do either of 2,
Kill process running on 3000
Or Use different port in your current app (like 3001, 5000, etc)
hi use this and kill the process directly;
netstat -ano | findstr :XXXX
XXXX <<<<----put the number of the port you have problem, system show it always, then when you press enter the system show you a message with a real port that one is what you need killing, so put this:
taskkill /F /PID XXXX
XXXX <<<<----the number of the port you want to kill and what system gave you after the first command
type this in command prompt
netstat -ano | findstr :XXXX <-THIS IS THE PORT NUMBER YOU used
And it will give you something like this
TCP 0.0.0.0:8080 || 0.0.0.0:0 || LISTENING | tttt
TCP [::]:8080 || [::]:0 || LISTENING || tttt<--thiss is the number you needed
then type this in command prompt
taskkill /PID tttt /F <--then enter
result will be something like -->
The process with PID tttt has been terminated.

Node.js Server not connecting to local mongodb instance

I have a node.js server that tries to connect to mongodb (working with mongoose):
mongoose.connect('mongodb://127.0.0.1:27017/ordry', {useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true}).then(result =>{ // 127.0.0.1:27017
console.log("Successfully connected to db!");
}).catch(error =>{
console.log("Error connecting to the database!")
})
Although mongodb is up and running, the connection fails.
I get the following logs:
Thu Jul 18 13:24:50.124 [initandlisten] options: { dbpath: "/home/pi/mongodb/" }
Thu Jul 18 13:24:50.166 [initandlisten] waiting for connections on port 27017
Thu Jul 18 13:24:50.166 [websvr] admin web console waiting for connections on port 28017
Thu Jul 18 13:25:45.647 [initandlisten] connection accepted from 127.0.0.1:50188
1 (1 connection now open)
Thu Jul 18 13:25:45.742 [conn1] end connection 127.0.0.1:50188 (0 connections now open)
EDIT Logs from Node.js:
[nodemon] starting `node ./server/server.js`
{ MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: Server at 127.0.0.1:27017 reports maximum wire version 0, but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)]
at Pool.<anonymous> (/home/pi/Desktop/Ordry API/node_modules/mongodb-core/lib/topologies/server.js:431:11)
at Pool.emit (events.js:198:13)
at connect (/home/pi/Desktop/Ordry API/node_modules/mongodb-core/lib/connection/pool.js:557:14)
at callback (/home/pi/Desktop/Ordry API/node_modules/mongodb-core/lib/connection/connect.js:109:5)
at runCommand (/home/pi/Desktop/Ordry API/node_modules/mongodb-core/lib/connection/connect.js:140:7)
at Connection.messageHandler (/home/pi/Desktop/Ordry API/node_modules/mongodb-core/lib/connection/connect.js:334:5)
at Connection.emit (events.js:198:13)
at processMessage (/home/pi/Desktop/Ordry API/node_modules/mongodb-core/lib/connection/connection.js:364:10)
at Socket.<anonymous> (/home/pi/Desktop/Ordry API/node_modules/mongodb-core/lib/connection/connection.js:533:15)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Conclusion
I am a bit unsure why it is not working here - especially as everything worked fine on windows.
I am using Raspbian on my Raspberry Pi 3b+.
Any help would be appreciated.
usually this happens because you didn't start mongod process before you try starting mongo shell.
1) Delete mongod.lock file from C:\Program
Files\MongoDB\Server\4.0\data
2) Start mongod server
In another terminal - start mongo shell
1) mongo
The error message you get:
reports maximum wire version 0, but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)
Is warning you to upgrade your MongoDB version to 2.6 at least, because it's not supported by the drivers used by the wire library which MongoDB depends on.
It seems that the version of MongoDB (2.4) provided by Raspbian is not supported anymore, so the best way to run a MongoDB server os a Raspberry Pi is to install another Linux distribution like Ubuntu or Arch Linux.

Port specified when running node is undefined

The port is specified when i ran the node project says undefined.
> sf-chain#1.0.0 dev E:\System\dev\node\sf-chain
> nodemon ./app "HTTP_PORT=3002" "P2P_PORT=5002" "PEERS=ws://localhost:5001"
[nodemon] 1.18.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./app HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001`
process.env.HTTP_PORT :: undefined
HTTP_PORT :: 3001
Listening for peer-to-peer connections on: 5001
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3001
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at Server._listen2 (net.js:1259:14)
at listen (net.js:1295:10)
at Server.listen (net.js:1391:5)
at EventEmitter.listen (E:\System\dev\node\sf-chain\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (E:\System\dev\node\sf-chain\app\index.js:28:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
[nodemon] app crashed - waiting for file changes before starting...
I ran the first instance using
npm run dev
const HTTP_PORT = process.env.HTTP_PORT || 3001;
The application starts successfully running on port 3001 since i didn't specify HTTP_PORT at the run command.
But when trying to run another instance by specifying HTTP_PORT using the below command
npm run dev HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001
I get this error.
Error: listen EADDRINUSE :::3001
Which means the HTTP_PORT specified at run time is seen as undefined that's why it's trying to using the 3001 port for the first instance.
Error: listen EADDRINUSE
- error comes when somthing is already running/listen on that port
Its because sometime instance of same nodejs acquire port and didnt kill/exit the process properly,
if you have Ubuntu check first which process is using port by
lsof -i :3001 // Port no.
then output will be some thing like this.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 2496 nasiruddin 13u IPv6 37696 0t0 TCP *:3001 (LISTEN)
kill using PID
kill 2496
then start again

Mongoose fails to connect to localhost: MongoError: failed to connect to server [127.0.0.1:27017]

I am working on a repo I downloaded from Github https://github.com/MessagingBots/MessengerBot, I know the js code is fine.
I have a config file directing the db URL to "mongodb://127.0.0.1:27017/messenger". (I haven't created the db yet)
I have the node app on Heroku, but Heroku Logs fail every time on mongodb connect.
2016-12-02T06:23:31.636339+00:00 app[web.1]: mongodb://127.0.0.1:27017/messenger
2016-12-02T06:23:31.672354+00:00 app[web.1]: CALLBACK URL IS
2016-12-02T06:23:31.672434+00:00 app[web.1]: https://cool-scrubland-89690.herokuapp.com/messages/receive/
2016-12-02T06:23:31.683394+00:00 app[web.1]: students
2016-12-02T06:23:31.701863+00:00 app[web.1]: listening on port 3124
2016-12-02T06:23:31.702052+00:00 app[web.1]: Application running at port 48594
2016-12-02T06:23:31.717752+00:00 app[web.1]:
2016-12-02T06:23:31.717764+00:00 app[web.1]: /app/node_modules/mongoose/node_modules/mongodb/lib/server.js:261
2016-12-02T06:23:31.717766+00:00 app[web.1]: process.nextTick(function() { throw err; })
2016-12-02T06:23:31.717767+00:00 app[web.1]: ^
2016-12-02T06:23:31.718744+00:00 app[web.1]: MongoError: failed to connect to server [127.0.0.1:27017] on first connect
2016-12-02T06:23:31.718746+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:313:35)
2016-12-02T06:23:31.718747+00:00 app[web.1]: at emitOne (events.js:96:13)
2016-12-02T06:23:31.718747+00:00 app[web.1]: at Pool.emit (events.js:188:7)
2016-12-02T06:23:31.718748+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:271:12)
2016-12-02T06:23:31.718749+00:00 app[web.1]: at Connection.g (events.js:291:16)
2016-12-02T06:23:31.718750+00:00 app[web.1]: at emitTwo (events.js:106:13)
2016-12-02T06:23:31.718750+00:00 app[web.1]: at Connection.emit (events.js:191:7)
2016-12-02T06:23:31.718751+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:165:49)
2016-12-02T06:23:31.718752+00:00 app[web.1]: at Socket.g (events.js:291:16)
2016-12-02T06:23:31.718752+00:00 app[web.1]: at emitOne (events.js:96:13)
2016-12-02T06:23:31.718753+00:00 app[web.1]: at Socket.emit (events.js:188:7)
2016-12-02T06:23:31.718754+00:00 app[web.1]: at emitErrorNT (net.js:1276:8)
2016-12-02T06:23:31.718754+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:74:11)
2016-12-02T06:23:31.718755+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:98:9)
2016-12-02T06:24:24.832476+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2016-12-02T06:24:24.832543+00:00 heroku[web.1]: Stopping process with SIGKILL
2016-12-02T06:24:24.958931+00:00 heroku[web.1]: State changed from starting to crashed
2016-12-02T06:24:24.949820+00:00 heroku[web.1]: Process exited with status 137
I have a mongod instance running. mongod properly connects to localhost and is stable
2016-12-02T14:02:26.733+0700 I CONTROL [initandlisten] MongoDB starting : pid=2706 port=27017 dbpath=/data/db 64-bit host=ME.local
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] db version v3.2.11
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2j 26 Sep 2016
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] allocator: system
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] modules: none
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] build environment:
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] distarch: x86_64
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] target_arch: x86_64
2016-12-02T14:02:26.735+0700 I CONTROL [initandlisten] options: {}
2016-12-02T14:02:26.738+0700 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2016-12-02T14:02:26.739+0700 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-12-02T14:02:27.758+0700 I CONTROL [initandlisten]
2016-12-02T14:02:27.759+0700 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2016-12-02T14:02:27.763+0700 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-12-02T14:02:27.763+0700 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2016-12-02T14:02:27.767+0700 I NETWORK [initandlisten] waiting for connections on port 27017
The problem might be related to Mongoose.
Anyone could help me out?
see if there is an old lock file present if present than remove it by
sudo rm /var/lib/mongodb/mongod.lock see if this works for you
sudo mongod --repair and (sudo start mongodb or sudo service mongodb start)
You haven't to precise mongodb:// in your mongoose connect call :
mongoose.connect("localhost:27017/mydbname");

Resources