Skip to content

Commit

Permalink
BE #1: Bootstrap the project using express.
Browse files Browse the repository at this point in the history
Create Config folder where all configs are declare.
Use consola to log in the console.
Use mongoose  to connect with the mongoDB.
  • Loading branch information
Atiqul Alam Rocky committed May 11, 2020
1 parent 4d61305 commit 6d8d0dc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ cache_temp/*
static_temp/*
.vscode
notice-config.js
package-lock.json
package-lock.json
.env
56 changes: 53 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -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');
});

6 changes: 6 additions & 0 deletions config/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('dotenv').config();

module.exports = {
DB: process.env.APP_DB,
PORT: process.env.PORT || 3000
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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<satyajit@cefalo.com>",
Expand All @@ -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"
},
Expand Down

0 comments on commit 6d8d0dc

Please sign in to comment.