How to use mongo URI in mongoLab and Heroku server
13,257
Solution 1
You can connect to the database using the following:
var mongoose = require('mongoose');
mongoose.connect('mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/<database name>');
But in your case I would replace mongodb://localhost/<database name>
with mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/<database name>
Solution 2
mongoURI = 'mongodb://localhost/test';
MONGOLAB_URI = "mongodb://<admin>:<admin>@ds3232.mlab.com:23213/abcd"
mongoose.connect(MONGOLAB_URI || mongoURI)
Author by
user2888686
Updated on June 04, 2022Comments
-
user2888686 about 2 years
I am using MongoDb and Nodejs. Nodejs is hosted on Heroku server and Mongodb is in MongoLab through Heroku add-on.
After installed MongoLab add-on, I received this URI:
mongodb://<dbuser>:<dbpassword>@ds053148.mongolab.com:53148/heroku_app18934798
I do not know how to use this URI, please help? Where to put it? what is
<dbuser>:<dbpassword
This is my config which is concern to mongoDB in
locallhost
In
server.js
var env = process.env.NODE_ENV || 'development', config = require('./config/config')[env], mongoose = require('mongoose'); var db = mongoose.connect(config.db);
In
express.js
app.use(express.session({ secret: 'thissecret', key:'express.sid', store: new mongoStore({ url: config.db, collection: 'sessions' }) }));
In
config.js
module.exports = { development: { db: 'mongodb://localhost/mean-dev', root: rootPath, app: { name: 'MEAN - A Modern Stack - Development' }, facebook: { clientID: "APP_ID", clientSecret: "APP_SECRET", callbackURL: "http://localhost:3000/auth/facebook/callback" } }, test: { db: 'mongodb://localhost/mean-test', root: rootPath, app: { name: 'MEAN - A Modern Stack - Test' }, facebook: { clientID: "APP_ID", clientSecret: "APP_SECRET", callbackURL: "http://localhost:3000/auth/facebook/callback" } }, production: { db: 'mongodb://localhost/mean', root: rootPath, app: { name: 'MEAN - A Modern Stack - Production' }, facebook: { clientID: "APP_ID", clientSecret: "APP_SECRET", callbackURL: "http://localhost:3000/auth/facebook/callback" } } };
-
Noel Widmer about 7 yearsCan you please explain what your solution is doing instead of just pasting a piece of code.
-
Omar Gonzalez almost 6 yearsthis is actually the best answer IMO, also pretty obvious by reading the code @N