-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.64 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
const express = require("express");
const exphbs = require("express-handlebars");
const path = require("path");
const bodyParser = require("body-parser");
const { request } = require("http");
const app = express();
const port = 4000;
//模擬的數據庫
let userDatabase = [
{ id: 1, name: "alice" },
{ id: 2, name: "jeff" },
];
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.engine("handlebars", exphbs.engine());
app.set("view engine", "handlebars");
app.set("views", path.join(__dirname, "views"));
// 單調的用json顯示userDatabase內容
app.get("/showUserDatabaseInJson", (request, response) => {
response.json(userDatabase);
});
// 當網址是/view-users時 顯示模擬數據庫內容
app.get("/view-userDatabase", (request, response) => {
response.render("userDatabase", { userDatabase });
});
// /users/1 會得到{"id":1,"name":"alice"}
app.get("/users/:id", (request, response) => {
const user = userDatabase.find((u) => u.id == request.params.id);
if (user) {
response.json(user);
} else {
response.status(400).send("user not found");
}
});
// 一個使用者介面讓人方便輸入id
app.get("/", (request, response) => {
response.render("viewUserForm");
});
//增加user表單會連過來 但不會跳轉到顯示所有user
app.post("/users", (request, response) => {
const newUser = { id: userDatabase.length + 1, name: request.body.name };
userDatabase.push(newUser);
response.status(201).json(newUser);
});
app.get("/new-user", (request, response) => {
response.render("newUserForm");
});
app.listen(port, () => {
console.log(`運行中 http://localhost:${port}`);
});