I'm attempting to connect to a mlab db for production and my local db for development. As far as I can tell my NODE_ENV
is set to production. But since I can't read it, I suspect that it's messed up somehow. In addition, I keep connecting to my dev db.
I run my mongod
instance. And in another cli, navigate to my app folder, which houses my app.js
file. I'm following a tutorial which says to run
process.env.NODE_ENV
However, I get that this command is not recognized. So I check my NODE_ENV
variable using: set
I get NODE_ENV=production
, which I had previously set. However. When I run my app using npm start
I still connect to my dev server. Here is my db.js code
:
var mongoose = require('mongoose');
var gracefulShutdown;
var dbURI = 'mongodb://localhost/Loc8r';
if(process.env.NODE_ENV == 'production'){
console.log('we are in production');
dbURI = 'mongodb://username:password@ds55555.mlab.com:55555/dbname'
}
mongoose.connect(dbURI);
// to address mongoose closing issues in windows
var readLine = require('readline');
if (process.platform === "win32"){
var rl = readLine.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on ("SIGINT", function(){
process.emit ("SIGINT");
});
}
// close Mongoose connection, passing in an anonymous function to run when closed
gracefulShutdown = function(msg, callback){
mongoose.connection.close(function(){
console.log("Mongoose disconnected through " + msg);
callback();
});
};
mongoose.connection.on('connected', function(){
console.log('Mongoose connected to ' + dbURI);
});
mongoose.connection.on('error', function(err){
console.log('Mongoose connection error: ' + err);
});
mongoose.connection.on('disconnected', function(){
console.log('Mongoose disconnected');
});
// for nodemon termination
process.once('SIGUSR2', function(){
gracefulShutdown('nodemon restart', function(){
process.kill(process.pid, 'SIGUSR2');
});
});
// for app termination
process.on('SIGINT', function(){
gracefulShutdown('app termination', function(){
process.exit(0);
});
});
// for HEroku termination
process.on('SIGTERM', function(){
gracefulShutdown('Heroku app shutdown', function(){
process.exit(0);
});
});
require('./locations');
Not sure where to start looking. I've exhausted my google searches.