diff --git a/.gitignore b/.gitignore index afd0003..a7b0065 100644 --- a/.gitignore +++ b/.gitignore @@ -55,4 +55,5 @@ cache_temp/* static_temp/* .vscode notice-config.js -package-lock.json \ No newline at end of file +package-lock.json +.env \ No newline at end of file diff --git a/app.js b/app.js index 9bfb81b..e210b3e 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,62 @@ const express = require('express'); const logger = require('morgan'); +const fs = require('fs') +const { connect } = require('mongoose') +const {success, error, info, log} = require('consola'); +//Bring the app constants +const {DB, PORT} = require('./config/application'); + +//Initialize the application const app = express(); +let retryAttemptCount = 5; +const startApp = async function(){ + try{ + //DB connection + info({ + message:'Trying to connect to the Database....', + badge:true + }) + + await connect(DB, { + useUnifiedTopology: true, + useNewUrlParser: true, + useFindAndModify: true + }) + + info({ + message:`Successfully conneted to the Database \n${DB}`, + badge: true + }); + + //Start to listen the port + app.listen(PORT, () => { + log({ + message:`Server started and listening on PORT: ${PORT}`, + badge:true + }) + }); + + }catch(err){ + error({ + message:`Unable to connect to Database /n${DB}\n${err}` + }); + + while(retryAttemptCount > 0){ + --retryAttemptCount; + startApp(); + } + + } + +} + +startApp() + + app.get('/', (req, res) => { res.send('Hello CMS!') }); -app.listen(3000, () => { - console.log('Server started and listening on PORT: 3000'); -}); \ No newline at end of file + diff --git a/config/application.js b/config/application.js new file mode 100644 index 0000000..ff3f6c6 --- /dev/null +++ b/config/application.js @@ -0,0 +1,6 @@ +require('dotenv').config(); + +module.exports = { + DB: process.env.APP_DB, + PORT: process.env.PORT || 3000 +} \ No newline at end of file diff --git a/package.json b/package.json index 64e8eba..3ff1600 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Simple CMS backend", "main": "app.js", "scripts": { - "start": "node app.js", + "start": "nodemon app.js", "test": "mocha" }, "author": "Satyajit Dey", @@ -28,7 +28,10 @@ ], "license": "ISC", "dependencies": { + "consola": "2.12.1", + "dotenv": "^8.2.0", "express": "4.17.1", + "mongoose": "5.9.13", "morgan": "1.10.0", "passport": "0.4.1" },