-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (80 loc) · 2.87 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
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
const express = require("express");
const fs = require("fs");
const path = require("path");
const { pool } = require("./db-connector.js");
const CONFIG = require("./config.json");
// Import routers
const customers = require("./routes/customers.js");
const rentalTransactions = require("./routes/rental-transactions.js");
const items = require("./routes/items.js");
const rentalItems = require("./routes/rental-items.js");
const discounts = require("./routes/discounts.js");
const liftPassTransactions = require("./routes/lift-pass-transactions.js");
const liftPassTypes = require("./routes/lift-pass-types.js");
const seasonDates = require("./routes/season-dates.js");
const app = express();
// Parse json post requests
app.use(express.json());
// Parse get data encoded in url
app.use(express.urlencoded({ extended: false }));
// Views set up
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
// Log access to the server
app.use((req, res, next) => {
console.log(`[${req.method}] (${req.ip}): ${req.url}`);
next();
});
// Serve static files in the public directory
app.use(express.static("public"));
// Use imported routers
app.use("/customers", customers);
app.use("/rental-transactions", rentalTransactions);
app.use("/items", items);
app.use("/rental-items", rentalItems);
app.use("/discounts", discounts);
app.use("/lift-pass-transactions", liftPassTransactions);
app.use("/lift-pass-types", liftPassTypes);
app.use("/season-dates", seasonDates);
// Reset the database
app.post("/reset", (req, res) => {
fs.readFile("./db/DDL.sql", async (err, data) => {
let query = data.toString();
let promises = [];
query.split(";").forEach((line, i) => {
// console.log("line:", line);
let promise = new Promise((resolve) =>
setTimeout(() => {
console.log("Sent line", i);
pool.query(line, (err, result) => {
if (err != null) {
// res.sendStatus(500);
console.log("Error resetting DB:", err);
resolve();
} else {
// res.sendStatus(200);
console.log("Reset DB:", result);
resolve();
}
});
}, 100 * i)
);
promises.push(promise);
});
await Promise.allSettled(promises);
res.sendStatus(200);
});
});
// No responses sent; not found
app.use((req, res) => {
console.log("[Not Found]:", req.url);
res.sendStatus(404);
});
// Start server
app.listen(CONFIG.PORT, () => {
console.log(`Listening on port: ${CONFIG.PORT}`);
pool.query("show tables", (err, result) => {
console.log("err:", err);
console.log("tables:", result);
});
});