CodeMed CodeMed - 5 months ago 32
Node.js Question

Point mongoos.connect at localhost mongodb

I am studying the MEAN stack by using this tutorial. But the tutorial connects to a remote mongodb installation. I have MongoDB running in the CentOS7 localhost.

How do I alter the mongoose connect line in server.js from the tutorial link above to connect to a localhost database instead of a remote database?
Here is the current line from

server.js
that needs to be changed to point to the localhost mongodb:

mongoose.connect('mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu');


A specific mongodb database has not been created yet. Do I need to create that also?

Answer

I'm fairly new at Mongo to, but I know how to connect to a local db. Basically I had to do the following:

mongod --dbpath [YOUR_DB_PATH]

  • Created a DB use mydb
  • With that you should have already a mongodb db instance looking for connections on default port. So you can change that line for this:

mongoose.connect('mongodb://localhost:27017/mydb');

Again, this is really basic and it is creating a mongo DB connection with all default options. So this will keep you rolling, but you may need to dig a bit more for custom options.

Hope this helps