-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
142 lines (125 loc) · 4.65 KB
/
index.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>API Demo</title>
</head>
<style>
* {
box-sizing: border-box;
}
body{
text-align: center;
margin: 0px;
font-family: Arial, Helvetica, sans-serif;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 25px;
height: 460px; /* Should be removed. Only for demonstration */
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
</style>
<body>
<h2 id="hostinfo" style="font-weight: 400; line-height: 40px;">Host Information </h2>
<h3 id="message" style="color: #646464;">Welcome to User Page</h3>
<div class="row">
<div class="column" style="background-color:#aaa;">
<h3>Insert new user to list</h3>
<form id="dataForm" method="POST" >
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<button type="submit">SUBMIT</button>
</form>
</div>
<div class="column" style="background-color:#bbb;">
<h3>User list</h3>
<table id="myTable" border="1px" style="margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody id="tableBody" >
<!-- Table content will be dynamically added here -->
</tbody>
</table>
<br>
<!-- <button id="getNewUser">GET NEW USER</button>-->
</div>
</div>
<br>
© 2023 Biswajit Nandi
<script>
// getNewUser.addEventListener('click', () => {
// fetchData()
// });
document.getElementById('dataForm').addEventListener('submit', async (event) => {
event.preventDefault();
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const response = await fetch('/insertData', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name, email })
});
const messageElement = document.getElementById('message');
const responseData = await response.text();
if (response.ok) {
messageElement.textContent = `User ${responseData}`;
document.getElementById("dataForm").reset();
// setTimeout(() => window.location.reload(), 2000); // Reload page after 2 seconds
await fetchData();
} else {
messageElement.textContent = `User ${responseData}`;
//messageElement.textContent = 'Error inserting data';
}
});
// User data factch
async function fetchData() {
try {
const response = await fetch('/fetchData');
const data = await response.json();
const tableBody = document.getElementById('tableBody');
tableBody.innerHTML = '';
data.forEach(item => {
const row = tableBody.insertRow();
const cell1 = row.insertCell(0);
const cell2 = row.insertCell(1);
cell1.innerHTML = item.name;
cell2.innerHTML = item.email;
})
} catch (error) {
console.error('Error fetching data:', error);
}
}
// Host info factch
async function hostinfo() {
try {
const response = await fetch('/hostinfo');
const hostinfo = await response.json();
// const hostinfoText = JSON.stringify({ hostinfo})
const hostdata = document.getElementById('hostinfo');
hostdata.innerHTML = `<h2>Data Server From Backend</h2> Host:<b> ${hostinfo.hostname}</b>, Private IP: <b>${hostinfo.privateIp}</b>, Public IP: <b>${hostinfo.publicIpAddress}</b>`;
// console.log(hostinfoText)
} catch (error) {
console.error('Error fetching data:', error);
}
}
hostinfo()
fetchData()
</script>
</body>
</html>