Skip to content

Commit 39fd25d

Browse files
committed
use post method
1 parent 4038117 commit 39fd25d

File tree

8 files changed

+115
-41
lines changed

8 files changed

+115
-41
lines changed

5.rest-api-express/public/css/app.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body {padding:0;}

5.rest-api-express/public/js/1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var x=10;

5.rest-api-express/public/js/jquery-3.6.3.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

5.rest-api-express/resource/data/users.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
"password" : "password3",
1919
"profession" : "clerk",
2020
"id": 3
21-
}
21+
}
2222
}

5.rest-api-express/server.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
var appRoot = require('app-root-path');
2+
const path = require('path');
3+
14
var express=require('express');
25
var app=express();
36
var fs = require("fs");
47

8+
59
var db=require('./service/database');
610

711

@@ -15,9 +19,12 @@ var user = {
1519
}
1620
}
1721

22+
// public permisssion
23+
app.use('/public', express.static('public'));
1824

1925
app.get('/',(req,res)=>{
20-
res.end( 'server on working!');
26+
// res.end( 'server on working!');
27+
res.sendFile(__dirname + "/views/" + "index.html");
2128
});
2229

2330

5.rest-api-express/test.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

5.rest-api-express/views/index.html

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<html lang="en">
2+
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
9+
<script src="/public/js/jquery-3.6.3.min.js"></script>
10+
<link rel="/public/css/app.css" href="styles.css">
11+
12+
<style type="text/css">
13+
.main {
14+
width: 800px;
15+
margin: 100px auto;
16+
padding-bottom: 200px;
17+
;
18+
}
19+
20+
.result {
21+
color: white;
22+
display: none;
23+
padding: 30px;
24+
25+
background-color: #71cfa4;
26+
}
27+
28+
input {
29+
width: 150px;
30+
height: 40px;
31+
}
32+
33+
form {
34+
background-color: aqua;
35+
padding: 30px;
36+
}
37+
</style>
38+
39+
<script type="text/javascript">
40+
$(document).ready(function () {
41+
42+
$("#btn_adduser").click(function () {
43+
// alert('call ajax method success!');
44+
$.post("/addUser", function (data, status) {
45+
// alert("Data: " + data + "\nStatus: " + status);
46+
// $("#result").fadeOut();
47+
$("#result").fadeOut(1000)
48+
$('#result').text(JSON.stringify(JSON.parse(data), undefined, 2));
49+
$("#result").fadeIn(1000);
50+
});
51+
52+
53+
54+
// fetch('/addUser', {
55+
// method: 'POST',
56+
// headers: {
57+
// 'Accept': 'application/json',
58+
// 'Content-Type': 'application/json'
59+
// },
60+
// body: JSON.stringify({ "id": 78912 })
61+
// })
62+
// .then(response => response.json())
63+
// .then(response => console.log(JSON.stringify(response)))
64+
65+
66+
67+
});
68+
69+
});
70+
71+
72+
</script>
73+
74+
75+
</head>
76+
77+
<body>
78+
<div class="main">
79+
80+
<p>Form action</p>
81+
<form action="/addUser" method="post">
82+
83+
<input type="submit" value="Submit">
84+
</form>
85+
86+
<div>
87+
<p>Ajax action</p>
88+
<input type="submit" id="btn_adduser" value="Submit Ajax">
89+
<p>Result</p>
90+
<pre id="result" class="result"></pre>
91+
</div>
92+
93+
</div>
94+
95+
96+
</body>
97+
98+
</html>

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"dependencies": {
3+
"app-root-path": "^3.1.0",
34
"cookie-parser": "^1.4.6",
4-
"express": "^4.18.2",
5+
"express": "^4.18.2",
56
"msnodesqlv8": "^3.0.1",
67
"mssql": "^9.0.1",
7-
"nodemon": "^2.0.20"
8+
"nodemon": "^2.0.20",
9+
"path": "^0.12.7"
810
}
911
}

0 commit comments

Comments
 (0)