-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
88 lines (70 loc) · 2.35 KB
/
main.js
File metadata and controls
88 lines (70 loc) · 2.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
const express = require("express");
const mongoose = require("mongoose");
const fs = require("fs");
const path = require("path");
const cors = require('cors');
const config = JSON.parse(fs.readFileSync("./config/config.json").toString());
const app = express();
app.use(cors());
const PORT = config.port;
const functions = require("./structs/functions.js");
const log = require("./structs/log.js");
let server;
mongoose.set('strictQuery', true);
mongoose.connect(config.mongodb.database)
.then(() => {
log.backend("App successfully connected to MongoDB!");
})
.catch(err => {
log.error("MongoDB failed to connect, please make sure you have MongoDB installed and running.");
throw err;
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Pas besoin pour le moment
// fs.readdirSync("./routes").forEach(fileName => {
// try {
// app.use(require(`./routes/${fileName}`));
// } catch (err) {
// log.error(`Routes Error: Failed to load ${fileName}`)
// }
// });
fs.readdirSync("./Api").forEach(fileName => {
try {
app.use(require(`./Api/${fileName}`));
} catch (err) {
log.error(`API Error: Failed to load ${fileName}`)
}
});
server = app.listen(PORT, () => {
log.backend(`Backend started listening on port ${PORT}`);
}).on("error", async (err) => {
if (err.code === "EADDRINUSE") {
log.error(`Port ${PORT} is already in use!\nClosing in 3 seconds...`);
await functions.sleep(3000);
process.exit(0);
} else {
throw err;
}
});
// redirection
app.use('/', express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/website.html'));
});
app.get('/dashboard', (req, res) => {
res.sendFile(path.join(__dirname, 'public/dashboard.html'));
});
app.get('/register', (req, res) => {
res.sendFile(path.join(__dirname, 'public/register.html'));
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public/login.html'));
});
app.get('/customer', (req, res) => {
res.sendFile(path.join(__dirname, 'public/customer.html'));
});
app.get('/404', (req, res) => {
res.sendFile(path.join(__dirname, 'public/404.html'));
});
module.exports = app;