better error handling

This commit is contained in:
2020-01-06 20:18:32 +01:00
parent 2f0adeda48
commit fbb9b42a7d

View File

@@ -12,13 +12,21 @@ app.listen(process.env.PORT, () => {
})
let db
const mongoUrl = `mongodb://${process.env.MONGO_SERVER}:27017`
MongoClient.connect(mongoUrl, { useNewUrlParser: true }).then(client => {
db = client.db(process.env.MONGO_DB)
console.log('Connected succesfully to server')
}).catch(err => console.log(err))
async function connectToMongo() {
try {
const mongoUrl = `mongodb://${process.env.MONGO_SERVER}:27017`
const client = await MongoClient.connect(mongoUrl, { useNewUrlParser: true })
db = client.db(process.env.MONGO_DB)
console.log('Connected succesfully to server')
} catch (e) {
console.log(e)
setTimeout(connectToMongo, 5000)
}
}
app.use(function(req, res, next) {
connectToMongo()
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
@@ -61,7 +69,7 @@ function dateFromObjectId(objectId) {
app.use(express.static(path.join(__dirname, '../dist')))
app.get('/*', function(req, res) {
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, '../dist/index.html'))
})