-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
128 lines (108 loc) · 3.35 KB
/
Copy pathserver.js
File metadata and controls
128 lines (108 loc) · 3.35 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require('dotenv').config(); // Load required sensitive variables from the .env file
// Import Frameworks + Modules
const express = require('express')
const path = require('path')
const { spawn } = require('child_process');
const cors = require('cors');
const allowedOrigins = ['https://rcpc-9s3f.onrender.com'];
const corsOptions = {
origin: function (origin, callback) {
if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
// Initialize MongoDB Client and Connect to it
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
const { error } = require('console');
const uri = process.env.DB;
const dbName = "rcpc-website-database";
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true
},
tlsAllowInvalidCertificates: true,
tls: true // Important for Atlas
});
(async () => {
try {
await client.connect();
console.log("Connected to client!");
} catch (err) {
console.error("Failed to connect to client:", err);
}
})();
const app = express() // Creates Express Instance
const port = 3000 // Define the Port
// Middleware
app.use(express.json());
app.use(cors(corsOptions));
app.use(express.urlencoded({ extended: true })); // For parsing the html form in /admin
app.use(express.static(path.join(__dirname, 'public')));
// Serve Static Starting Frontend
app.get('/', (req, res,) => {
res.send(express.static('index.html'))
})
app.listen(port, () => {
console.log('Listening on *:3000');
init();
})
/* /database endpoints:
* - get endpoint for read operations
* - post endpoint for insert operations
* - delete endpoint for remove operations
*
* endpoints are used by FE to get/post/delete data
* request arguments:
* - path arg: the collection
* - query args: key/value pairs used for querying the collection
*/
app.get('/api/directors', async (_, res) => {
try {
const db = client.db(dbName);
const collection = db.collection('directors');
const data = await collection.find().toArray();
res.status(200).json({
ok: true,
data: data
});
} catch(error) {
res.status(500).json({
ok: false,
error: error
});
}
});
app.post('/admin', (req, res) => {
const userInput = req.body.password; // gets user input
getPassword().then(
(hashedPassword) => {
if (hashedPassword == userInput) {
res.redirect('/admin/dashboard');
} else {
res.send(`
<script>
alert("Incorrect password.");
</script>
`);
}
},
(failure) => {
console.log("could not resolve promise", failure);
res.status(500).json({
message: "An error occurred while connecting to the database.",
error: failure
});
}
)
});
/* /admin/dashboard endpoint:
* serves the private dashboard directory not served in the public directory
*/
app.get('/admin/dashboard', (req, res) => {
res.sendFile(path.join(__dirname, 'public/admin/dashboard/', 'index.html'));
});