Skip to content

Commit e6c8c62

Browse files
authored
フォームの章に問題を追加 (#445)
1 parent 1850c89 commit e6c8c62

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import express from "express";
2+
const app = express();
3+
4+
app.use(express.static("static"));
5+
app.use(express.urlencoded({ extended: true }));
6+
7+
const PORT = 3000;
8+
9+
const userCredentials = [
10+
{ username: "tanaka", password: "kfae12F@" },
11+
{ username: "sato", password: "faewbnpE3b=" },
12+
{ username: "endo", password: "fEnEoB131b" },
13+
];
14+
15+
app.post("/login", (request, response) => {
16+
const username = request.body.username;
17+
const password = request.body.password;
18+
19+
const userCredential = userCredentials.find(
20+
(userCredential) =>
21+
userCredential.username == username &&
22+
userCredential.password == password,
23+
);
24+
if (userCredential === undefined) {
25+
response.send("ログインに失敗しました。");
26+
} else {
27+
response.send("ログインに成功しました。");
28+
}
29+
});
30+
31+
app.listen(PORT);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>ログイン</title>
6+
</head>
7+
<body>
8+
<form action="/login" method="post">
9+
<input type="text" name="username" placeholder="ユーザー名" />
10+
<input type="password" name="password" placeholder="パスワード" />
11+
<button>ログイン</button>
12+
</form>
13+
</body>
14+
</html>

docs/3-web-servers/05-form/index.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,33 @@ app.listen(3000);
117117
このページの URL を見てみましょう。特に、クエリパラメータの部分に注目してみましょう。
118118
![URLのクエリパラメータ](form-url.png)
119119

120-
## 課題
120+
## 課題1
121+
122+
ログイン画面を作ってみましょう。以下のような画面を作成してください。
123+
124+
![ログイン画面](./work1.png)
125+
126+
ユーザー名とパスワードを入力して、ログインボタンを押すと、`/login` に移動し、ログインに成功した場合は `ログインに成功しました。` と表示し、失敗した場合は `ログインに失敗しました。` と表示するようにしてください。(POSTリクエストを使ってみましょう。)
127+
128+
:::tip
129+
ユーザー名とパスワードは以下のように配列で管理してみましょう。
130+
131+
```javascript
132+
const userCredentials = [
133+
{ username: "tanaka", password: "kfae12F@" },
134+
{ username: "sato", password: "faewbnpE3b=" },
135+
{ username: "endo", password: "fEnEoB131b" },
136+
];
137+
```
138+
139+
`Array`オブジェクトの`find`メソッドを使用して、ユーザー名とパスワードが一致する要素を探してみましょう。
140+
:::
141+
142+
### 解答例
143+
144+
<ViewSource url={import.meta.url} path="_samples/login" />
145+
146+
## 課題2
121147

122148
書籍検索システムを作ってみましょう。まずは、配列に本のデータを登録します。
123149

10.8 KB
Loading

0 commit comments

Comments
 (0)