-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
87 lines (63 loc) · 2.15 KB
/
server.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
const express = require('express');
const path = require('path');
const fs = require("fs");
const app = express();
const PORT = 3000;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'home.html')));
app.get('/tables', (req, res) => res.sendFile(path.join(__dirname, '/tables.html')));
app.get('/reserve', (req, res) => res.sendFile(path.join(__dirname, '/reserve.html')));
// app.get('/api/tables', (req, res) => res.json(reservation));
app.get('/api/waitlist', (req, res) => res.json(waitList));
app.get("/api/tables", (req, res) => {
fs.readFile(__dirname + "/db/db.json", 'utf8', (err, data) => {
if (err) {
return console.log(err);
}
console.log(data);
res.json(JSON.parse(data));
});
});
const reservation = [
{
customerID: "123",
customerName: "kemal",
customerEmail: "kemal@asd",
phoneNumber: "6132321213"
}
]
const waitList = [
{
customerID: "2223",
customerName: "tatyana",
customerEmail: "kasdasdsasdl@asd",
phoneNumber: "613232231123211231231321213"
}
]
app.post('/api/tables', (req, res) => {
const newReservation = req.body;
if (reservation.length >= 5) {
app.post('/api/waitlist', (req, res) => {
console.log(newReservation);
waitList.push(newReservation);
res.json(newReservation);
});
} else {
console.log(newReservation);
const reserveList = JSON.parse(fs.readFileSync("./db/db.json", "utf8"));
reserveList.push(newReservation);
fs.writeFileSync("./db/db.json", JSON.stringify(reserveList));
res.json(reserveList);
}
});
app.get("/api/tables", (req, res) => {
fs.readFile(__dirname + "/db/db.json", 'utf8', (err, data) => {
if (err) {
return console.log(err);
}
console.log(data);
res.json(JSON.parse(data));
});
});
app.listen(PORT, () => console.log(`App listening on PORT ${PORT}`));