9

I disable the internet connection and run the node server npm start, then it throws the error:

enter image description here

And now enabling internet connection and running the server works fine:

enter image description here

So, I want to confirm if this really needs to be connected to the internet while starting the server?

If it really needs internet connection, then is there any idea to run the server offline?

If I connect to the internet and run the server and there after plug out the internet connection then there's nothing goes wrong. Everything works fine. I don't know why just running the server requires the internet connection.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 1
    Are you trying to connect to an external mongo database? Your error says that is where you are blowing up. – ThrowsException Jul 09 '15 at 19:09
  • Where is the Mongo server running? On the same machine as the Node server? – Some guy Jul 09 '15 at 19:12
  • If you disable the internet connection (which I assume does not strictly mean you are disabling the network adapter completely), does the server have a networking route to connect to that mongodb? – Luis Delgado Jul 09 '15 at 19:12
  • Im only going by the error printed. It looks like it fails when mongoose is trying to connect to the database. Is your Mongo database on your local machine? Check your settings to make sure it is not trying to connect to a Mongo instance on another machine. – ThrowsException Jul 09 '15 at 19:13
  • that's what were asking. Are you hooked up to an external mongodb somewhere. Are you sure you are running using the developement environment provided by MEAN. Double check your dev environment settings and make sure nothing is overwritten. – ThrowsException Jul 09 '15 at 19:21
  • Did you try to run Node with `node` command instead `npm`? `node server.js` – michelem Jul 09 '15 at 19:36
  • I got it working. Check my answer. Amazed though. – Bhojendra Rauniyar Jul 09 '15 at 20:40
  • Possible duplicate of [Mongoose Can't Connect Without Internet](http://stackoverflow.com/questions/29178484/mongoose-cant-connect-without-internet) – Yaacov Apr 24 '17 at 18:14

1 Answers1

17

OMG! It's really cool! I got it working offline after hard research in my code using 127.0.0.1 instead of localhost for mongo uri.

Using localhost needed for me to connect to the internet.

And using 127.0.0.1 worked in both condition i.e. with internet connection and without internet connection.

But, really I'm totally amazed of this.


I found the really cool topic on this which made me clear about this.

If you use 127.0.0.1, then (intelligent) software will just turn that directly into an IP address and use it.

And there's no guarantee that your hosts file will actually be used for that resolution (first, or at all) so localhost may become a totally different IP address.

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 2
    That's because localhost needs to be resolved. by using 127.0.0.1 you are skipping the lookup part in your host file. therefore no connection needed. – ThrowsException Jul 11 '15 at 15:50