Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mongo URI in mongoLab and Heroku server

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"
        }

    }
};
like image 208
user2888686 Avatar asked Oct 02 '22 13:10

user2888686


1 Answers

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>

like image 189
Samuel O'Malley Avatar answered Oct 13 '22 11:10

Samuel O'Malley