Hiren Hiren - 10 months ago 77
Node.js Question

Failing to connect to MongoDB on 27017 in NodeJs

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...

Answer Source

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.