-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (44 loc) · 1.46 KB
/
Copy pathindex.js
File metadata and controls
58 lines (44 loc) · 1.46 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
// Constants
const express = require("express");
const path = require("path");
const http = require("http");
const socketio = require("socket.io");
const fs = require("fs");
const bodyParser = require('body-parser');
const root = path.resolve(path.dirname(""));
const logHandler = require(`${root}/Util/loghandler.js`);
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use(express.static(root + '/Public'));
app.use(express.json());
// Socket router
io.on("connection", (socket) => {
// Iterating through each module and passing socket to them
fs.readdirSync(`${root}/Socket`).forEach( (filename) => {
require(`${root}/Socket/${filename}`).invoke(socket);
});
});
// Main
app.get("/", (req, res) => {
res.sendFile(`${root}/Public/HTML/index.html`);
});
// Page routing
fs.readdirSync(`${root}/Pages`).forEach( (filename) => {
// Iterating through each module and passing the request and response
app.all(`/${filename.split(".")[0]}`, urlencodedParser, (req, res) => {
require(`${root}/Pages/${filename}`).invoke(req, res);
});
});
// Error handling
app.use((error, req, res, next) => {
logHandler.log(error, "Error");
});
// Log initialization
logHandler.clearLogs("Error");
logHandler.clearLogs("Authentication");
logHandler.startLogging("Error");
logHandler.startLogging("Authentication");
server.listen(3001);
console.log();