I have a simple node with mongo (via mongojs) app that is developed locally and deployed on heroku. In my development environment, i want to use a local instance of mongo, while in production, I would like to use the instance heroku provides to me via "process.env.MONGOLAB_URI".
My current approach is that I would set the datavase url depending on the environment variable, but how do i actually go into production mode? Moreover, how can i configure this so that when i develop on my local machine its development mode, when i upload to heroku its production mode?
app.configure('production', function(){
// ...
databaseUrl = "mydb"; // the default
});
app.configure('development', function(){
// ...
databaseUrl = process.env.MONGOLAB_URI;
});
db = require("mongojs").connect(databaseUrl);
Set the NODE_ENV environment variable to "development" on your local environment, and set it to "production" on Heroku. https://devcenter.heroku.com/articles/nodejs#setting-node-env
You can also access your online database locally by starting your app by adding the following:
var mongoose = require( 'mongoose' );
var dbURI = 'mongodb://localhost/Loc8r';
if (process.env.NODE_ENV === 'production') {
dbURI= process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);
And starting your app with "NODE_ENV=production nodemon bin/www"