Skip to content

Commit

Permalink
Decoupled app from index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gunvantsr committed Sep 10, 2022
1 parent aa6ce49 commit 481f48e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
32 changes: 32 additions & 0 deletions boilerplates/javascript/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require('express');
const cors = require('cors');

const app = express();

app.use(cors());
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);

app.use('/health', (req, res) => {
const healthCheck = {
uptime: process.uptime(),
message: 'OK',
timestamp: Date.now(),
};
try {
res.send(healthCheck);
} catch (error) {
healthCheck.message = error;
res.status(503), send();
}
});

const PORT = '5000' || process.env.PORT;
module.exports = {
app,
PORT,
};
2 changes: 0 additions & 2 deletions boilerplates/javascript/config/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require('dotenv').config();

DB_URI = process.env.DB_URI;

const DB_URI = process.env.DB_URI;

module.exports = {
Expand Down
15 changes: 15 additions & 0 deletions boilerplates/javascript/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const mongoose = require('mongoose');
const { DB_URI } = require('./config');

const dbConnection = async () => {
await mongoose
.connect(DB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Database connected successfully!');
})
.catch((error) => {
console.log(error);
});
};

module.exports = dbConnection;
47 changes: 8 additions & 39 deletions boilerplates/javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,11 @@
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const { app, PORT } = require('./app');
const dbConnection = require('./config/db');

const app = express();
const PORT = '5000' || process.env.PORT;
const { DB_URI } = require('./config/config');

mongoose
.connect(DB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Database connected successfully!');
})
.catch((error) => {
console.log(error);
const bootstrap = () => {
app.listen(PORT, () => {
dbConnection();
console.log(`app is listening on http://localhost:${PORT}..`);
});
};

app.use(cors());
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);

app.use('/health', (req, res) => {
const healthCheck = {
uptime: process.uptime(),
message: 'OK',
timestamp: Date.now(),
};
try {
res.send(healthCheck);
} catch (error) {
healthCheck.message = error;
res.status(503), send();
}
});

app.listen(PORT, () => {
console.log(`app is listening on http://localhost:${PORT}..`);
});
bootstrap();
Empty file.

0 comments on commit 481f48e

Please sign in to comment.