Skip to content

Commit

Permalink
Updated server.js to handle MongoDb Exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
NidhiT14 committed Sep 28, 2018
1 parent 809ff52 commit 1d2e4b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
26 changes: 24 additions & 2 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@ import Info from './models/Info';

const app = express();
const router = express.Router();
var port = 4000;
var port = 3000;
app.use(cors());
// var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect('mongodb://localhost:27017/form',{ useNewUrlParser: true } );
var dbURL = 'mongodb://127.0.0.1:27017/form';
mongoose.connect(dbURL, { useNewUrlParser: true } );

const connection = mongoose.connection;

connection.on('error', function (err) {
// If first connect fails because mongod is down, try again later.
// This is only needed for first connect, not for runtime reconnects.
// See: https://github.com/Automattic/mongoose/issues/5169
if (err.message && err.message.match(/failed to connect to server .* on first connect/)) {
console.log(new Date(), String(err));

// Wait for a bit, then try to connect again
setTimeout(function () {
console.log("Retrying first connect...");
connection.openUri(dbURL).catch(() => {});
// Why the empty catch?
// Well, errors thrown by db.open() will also be passed to .on('error'),
// so we can handle them there, no need to log anything in the catch here.
// But we still need this empty catch to avoid unhandled rejections.
}, 20 * 1000);
} else {
// Some other error occurred. Log it.
console.error(new Date(), String(err));
}
});
connection.once('open', () => {
console.log('MongoDB database connection established successfully!');
});
Expand Down
5 changes: 4 additions & 1 deletion src/app/home/info.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { HttpClient } from '@angular/common/http';
})
export class InfoService {

uri = 'http://localhost:4000';
// uri = 'http://54.226.207.32:4000';
// uri = 'http://localhost:4000';
//when pushing to github uncomment this
uri = 'http://54.197.12.23:3000';

constructor(private http: HttpClient) { }

Expand Down

0 comments on commit 1d2e4b8

Please sign in to comment.