Skip to content

Commit

Permalink
indent fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoythander committed Aug 26, 2018
1 parent 060e88c commit 657f11c
Showing 1 changed file with 80 additions and 80 deletions.
160 changes: 80 additions & 80 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,83 +22,83 @@ var token = require('./routes/token');
// Require mongoose
var mongoose = require('mongoose');
var dbOptions = {
keepAlive: 200,
autoReconnect: true,
reconnectInterval: 3000,
useNewUrlParser: true
keepAlive: 200,
autoReconnect: true,
reconnectInterval: 3000,
useNewUrlParser: true
};

var reconnectTries = 0;
var trialDelay = 1;

function delayString(seconds) {
var sec = seconds % 60;
seconds -= sec;
var min = seconds / 60;
var temp = min;
min %= 60;
var hour = (temp - min) / 60;

var str = '';
if (hour>0) {
str += hour;
str += ' hour'
if (hour>1) str += 's';
if (min>0 || sec>0) str += ', ';
}
if (min>0) {
str += min;
str += ' minute'
if (min>1) str += 's';
if (sec>0) str += ', ';
}
if (sec>0) {
str += sec;
str += ' second'
if (sec>1) str += 's';
}
return str;
var sec = seconds % 60;
seconds -= sec;
var min = seconds / 60;
var temp = min;
min %= 60;
var hour = (temp - min) / 60;

var str = '';
if (hour>0) {
str += hour;
str += ' hour'
if (hour>1) str += 's';
if (min>0 || sec>0) str += ', ';
}
if (min>0) {
str += min;
str += ' minute'
if (min>1) str += 's';
if (sec>0) str += ', ';
}
if (sec>0) {
str += sec;
str += ' second'
if (sec>1) str += 's';
}
return str;
}
function dbConnect() {
console.log('Connecting database ...');
mongoose.connect(
// Replace CONNECTION_URI with your connection uri
'CONNECTION_URI',
dbOptions
).then(function() {
console.log('Database connection successful !!!');
console.log('Server is fully functional');
}, function(err) {
console.log('Database connection failed');

reconnectTries++;
console.log('Reconnecting after '+delayString(trialDelay));
console.log('Reconnect trial: '+reconnectTries);
console.log('');
delay(trialDelay*1000).then(function() {
trialDelay += trialDelay;
if (trialDelay>7200) trialDelay = 7200;
// enable recurtion
dbConnect();
});
});
console.log('Connecting database ...');
mongoose.connect(
// Replace CONNECTION_URI with your connection uri
'CONNECTION_URI',
dbOptions
).then(function() {
console.log('Database connection successful !!!');
console.log('Server is fully functional');
}, function(err) {
console.log('Database connection failed');

reconnectTries++;
console.log('Reconnecting after '+delayString(trialDelay));
console.log('Reconnect trial: '+reconnectTries);
console.log('');
delay(trialDelay*1000).then(function() {
trialDelay += trialDelay;
if (trialDelay>7200) trialDelay = 7200;
// enable recurtion
dbConnect();
});
});
}
dbConnect();

// CORS Config
var whitelist = [
undefined, // POSTMAN Support
'http://localhost:XXXX', // DEV Support
'https://example.com' // Production Support
undefined, // POSTMAN Support
'http://localhost:XXXX', // DEV Support
'https://example.com' // Production Support
]
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error(origin + ' is not allowed to access'))
}
}
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error(origin + ' is not allowed to access'))
}
}
}

// start express app
Expand All @@ -118,7 +118,7 @@ app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(bodyParser.urlencoded({
extended: false
extended: false
}));
app.use(bodyParser.json());

Expand All @@ -129,9 +129,9 @@ app.use('/token', token);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
var err = new Error('Not Found');
err.status = 404;
next(err);
});


Expand All @@ -140,25 +140,25 @@ app.use(function(req, res, next) {
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.send({
state: 'failure',
message: err.message,
error: err
});
});
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.send({
state: 'failure',
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.send({
state: 'failure',
message: err.message,
error: err
});
res.status(err.status || 500);
res.send({
state: 'failure',
message: err.message,
error: err
});
});

module.exports = app;

0 comments on commit 657f11c

Please sign in to comment.