-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (48 loc) · 1.74 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import cookieParser from "cookie-parser";
import cors from "cors";
import path from "path";
import qs from "querystring";
import fs from "fs";
import { config } from "dotenv";
import express_ejs from "ejs";
import express, { json, urlencoded } from "express";
import { connect, connection } from "mongoose";
import router from "./routes/index";
const app = express();
var db_error;
try {
fs.statSync(path.join(__dirname, "/config/.env"));
config({ path: path.join(__dirname, "/config/.env") });
app.use(cors());
app.use(json({ limit: "10mb" }));
app.use(urlencoded({ extended: false }));
app.use(cookieParser());
app.use("/", router);
app.engine("html", express_ejs.renderFile);
//#DATABASE CONNECT WITH SSL ENCRYPTION FUNCTION
const db_connect = () => {
const mongouri = `mongodb://${process.env.DB_USER}:${qs.escape(process.env.DB_PASSWORD)}@${process.env.DB_HOST}/${process.env.DB_NAME}?authSource=${process.env.DB_AUTH_DB_NAME}`;
connect(mongouri, {
useNewUrlParser: true,
useUnifiedTopology: true,
tls: true,
tlsCAFile: process.env.DB_SSL_CERT,
tlsCertificateKeyFile: process.env.DB_SSL_KEY
}, (err) => {
db_error = err;
if (err) throw err;
console.log("[DB] Database connected via TCP/IP on port 27017 with TLS encryption");
});
};
db_connect();
}
catch (err) {
if (err.code === "ENOENT") throw new Error ("missing \'.env\' file in \"config\" folder. please modify \'.env.sample\' file.");
throw(err);
}
connection.on("disconnected",() => {
console.log("[DB] Database disconnect. Trying to reconnect...");
db_error = "disconnected";
db_connect();
});
export {app , db_error};