-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
30 lines (27 loc) · 882 Bytes
/
db.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
const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');
function initializeDatabase() {
function readPassFile() {
try {
const dbpw = fs.readFileSync('./secrets/mongo_root_password.txt', 'utf8');
if (dbpw) {console.log("dbpw read successfully: " + dbpw);}
return dbpw;
} catch (err) {
console.error("dbpw not read successfully, error: " + err);
}
}
function connect() {
const dbpw = readPassFile();
const url = `mongodb://root:${dbpw}@localhost:27017`;
try {
const client = new MongoClient(url);
console.log("Connected successfully to client!")
return client;
} catch (err) {
console.error("client not created successfully, error: " + err);
}
}
return connect();
}
const mongo = initializeDatabase();
module.exports = mongo;