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