Skip to content

Commit 8e5dbbe

Browse files
committed
Merge pull request meanjs#289 from lirantal/refactor-mongodb-connection
Refactor mongodb connection
2 parents f49f1cf + 90e05f6 commit 8e5dbbe

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

config/env/development.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict';
22

33
module.exports = {
4-
db: 'mongodb://localhost/mean-dev',
4+
db: {
5+
uri: 'mongodb://localhost/mean-dev',
6+
options: {
7+
user: '',
8+
pass: ''
9+
}
10+
},
511
log: {
612
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
713
format: 'dev',

config/env/production.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict';
22

33
module.exports = {
4-
db: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean',
4+
db: {
5+
uri: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://' + (process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost') + '/mean',
6+
options: {
7+
user: '',
8+
pass: ''
9+
}
10+
},
511
log: {
612
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
713
format: 'combined',

config/env/secure.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
module.exports = {
44
port: 8443,
5-
db: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/mean',
5+
db: {
6+
uri: process.env.MONGOHQ_URL || process.env.MONGOLAB_URI || 'mongodb://localhost/mean',
7+
options: {
8+
user: '',
9+
pass: ''
10+
}
11+
},
612
log: {
713
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'
814
format: 'combined',

config/env/test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict';
22

33
module.exports = {
4-
db: 'mongodb://localhost/mean-test',
4+
db: {
5+
uri: 'mongodb://localhost/mean-test',
6+
options: {
7+
user: '',
8+
pass: ''
9+
}
10+
},
511
port: 3001,
612
log: {
713
// Can specify one of 'combined', 'common', 'dev', 'short', 'tiny'

server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ var init = require('./config/init')(),
1313
*/
1414

1515
// Bootstrap db connection
16-
var db = mongoose.connect(config.db, function(err) {
16+
var db = mongoose.connect(config.db.uri, config.db.options, function(err) {
1717
if (err) {
1818
console.error(chalk.red('Could not connect to MongoDB!'));
1919
console.log(chalk.red(err));
2020
}
2121
});
22+
mongoose.connection.on('error', function(err) {
23+
console.error(chalk.red('MongoDB connection error: ' + err));
24+
process.exit(-1);
25+
}
26+
);
2227

2328
// Init the express application
2429
var app = require('./config/express')(db);
@@ -37,7 +42,7 @@ console.log('--');
3742
console.log(chalk.green(config.app.title + ' application started'));
3843
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
3944
console.log(chalk.green('Port:\t\t\t\t' + config.port));
40-
console.log(chalk.green('Database:\t\t\t' + config.db));
45+
console.log(chalk.green('Database:\t\t\t' + config.db.uri));
4146
if (process.env.NODE_ENV === 'secure') {
4247
console.log(chalk.green('HTTPs:\t\t\t\ton'));
4348
}

0 commit comments

Comments
 (0)