-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (70 loc) · 1.96 KB
/
index.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
// setup express app
const express = require('express')
const app = express()
const port = 3000
// middleware to get the request body
app.use(express.json());
app.use(express.urlencoded());
const path = require("path");
const fs = require("fs");
// show case the main file
app.get("/", (req, res,next) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get("/data_page", (req, res,next) => {
res.sendFile(path.join(__dirname, "public", "data.html"));
});
//for using css in index.html
app.use(express.static(__dirname + '/public'));
app.post("/", (req, res,next) => {
const users = require("./public/data/data.json");
const {vAgency,vSize,vArea,vWork} = req.body;
var d = new Date();
var hour = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();
let user = {
name: hour+":"+min+":"+sec,
agency: vAgency,
size: vSize,
area: vArea,
work: vWork
};
users.push(user);
fs.writeFile('./public/data/data.json', JSON.stringify(users), function (err) {
if (err) throw err;
console.log('Updated!');
});
res.redirect('/')
});
app.get("/data", (req, res,next) => {
fs.readFile("./public/data/data.json", function(err, data) {
// if (err) throw err;
// Converting to JSON
const users = JSON.parse(data);
console.log(users);
});
res.sendFile(path.join(__dirname, "public", "data.html"));
});
// remove id based on time
app.post('/data_remove',(req,res)=>{
const {time} = req.body;
const users = require("./public/data/data.json");
for( var i = 0; i < users.length; i++){
if ( users[i].name === time) {
users.splice(i, 1);
}
}
fs.writeFile('./public/data/data.json', JSON.stringify(users), function (err) {
if (err) throw err;
console.log('Updated!');
});
res.redirect('/data');
})
app.get('*', function (req, res) {
res.redirect('/');
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
console.log(`http://localhost:${port}`)
})