Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.configure(function configureMongoDbConnection() {
- var connectionString = 'mongodb://localhost:27017/test',
- sleep = 1000,
- timer;
- db.on('connecting', function() {
- console.log('connecting to MongoDB...');
- });
- db.on('error', function(error) {
- console.error('Error in MongoDb connection: ' + error);
- mongoose.disconnect();
- });
- db.on('connected', function() {
- console.log('MongoDB connected!');
- });
- db.once('open', function() {
- clearTimeout(timer);
- connect.time = 0;
- console.log('MongoDB connection opened!');
- });
- db.on('reconnected', function () {
- console.log('MongoDB reconnected!');
- });
- db.on('disconnected', function() {
- console.log('MongoDB disconnected!');
- connect.time += sleep;
- timer = setTimeout(connect, sleep);
- });
- function connect() {
- if(connect.time > 22000) {
- console.error('giving up on db connection');
- return;
- }
- mongoose.connect(connectionString, {server:{auto_reconnect:true}});
- }
- connect.time = 0;
- connect();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement