Merge pull request #289 from lirantal/refactor-mongodb-connection

Refactor mongodb connection
This commit is contained in:
Liran Tal
2014-12-27 18:04:28 +02:00
5 changed files with 35 additions and 6 deletions

View File

@@ -1,7 +1,13 @@
'use strict';
module.exports = {
db: 'mongodb://localhost/mean-dev',
db: {
uri: 'mongodb://localhost/mean-dev',
options: {
user: '',
pass: ''
}
},
log: {
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
format: 'dev',

View File

@@ -1,7 +1,13 @@
'use strict';
module.exports = {
db: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean',
db: {
uri: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean',
options: {
user: '',
pass: ''
}
},
log: {
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
format: 'combined',

View File

@@ -2,7 +2,13 @@
module.exports = {
port: 8443,
db: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/mean',
db: {
uri: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/mean',
options: {
user: '',
pass: ''
}
},
log: {
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
format: 'combined',

8
config/env/test.js vendored
View File

@@ -1,7 +1,13 @@
'use strict';
module.exports = {
db: 'mongodb://localhost/mean-test',
db: {
uri: 'mongodb://localhost/mean-test',
options: {
user: '',
pass: ''
}
},
port: 3001,
log: {
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'

View File

@@ -13,12 +13,17 @@ var init = require('./config/init')(),
*/
// Bootstrap db connection
var db = mongoose.connect(config.db, function(err) {
var db = mongoose.connect(config.db.uri, config.db.options, function(err) {
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(chalk.red(err));
}
});
mongoose.connection.on('error', function(err) {
console.error(chalk.red('MongoDB connection error: ' + err));
process.exit(-1);
}
);
// Init the express application
var app = require('./config/express')(db);
@@ -37,7 +42,7 @@ console.log('--');
console.log(chalk.green(config.app.title + ' application started'));
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
console.log(chalk.green('Port:\t\t\t\t' + config.port));
console.log(chalk.green('Database:\t\t\t' + config.db));
console.log(chalk.green('Database:\t\t\t' + config.db.uri));
if (process.env.NODE_ENV === 'secure') {
console.log(chalk.green('HTTPs:\t\t\t\ton'));
}