Skip to content

Commit ef1375c

Browse files
committed
EJS脱却 - cookie
1 parent c00cf05 commit ef1375c

File tree

5 files changed

+96
-116
lines changed

5 files changed

+96
-116
lines changed

docs/3-web-servers/08-cookie/_samples/profile/index.ejs renamed to docs/3-web-servers/08-cookie/_samples/profile/index.html

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
<!doctype html>
2-
<html lang="ja">
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>profile</title>
6-
</head>
7-
<body>
8-
<form method="post" action="/login">
9-
<input name="username" />
10-
<input name="password" type="password" />
11-
<button>ログイン</button>
12-
</form>
13-
<p><%= warning %></p>
14-
<a href="/resister">新規登録</a>
15-
</body>
16-
</html>
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>profile</title>
6+
</head>
7+
<body>
8+
<form method="post" action="/login">
9+
<input name="username" />
10+
<input name="password" type="password" />
11+
<button>ログイン</button>
12+
</form>
13+
<p>{warning}</p>
14+
<a href="/resister">新規登録</a>
15+
</body>
16+
</html>

docs/3-web-servers/08-cookie/_samples/profile/login.ejs renamed to docs/3-web-servers/08-cookie/_samples/profile/login.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<!doctype html>
2-
<html lang="ja">
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>profile</title>
6-
</head>
7-
<body>
8-
<p>ようこそ!<%= username %>さん!</p>
9-
<a href="/profile">プロフィールを表示</a>
10-
</body>
11-
</html>
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>profile</title>
6+
</head>
7+
<body>
8+
<p>ようこそ!{username}さん!</p>
9+
<a href="/profile">プロフィールを表示</a>
10+
</body>
11+
</html>

docs/3-web-servers/08-cookie/_samples/profile/main.mjs

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import express from "express";
22
import cookieParser from "cookie-parser";
33
import fs from "fs";
4-
import ejs from "ejs";
54

65
const app = express();
76
app.use(express.urlencoded({ extended: true }));
@@ -11,25 +10,21 @@ import { PrismaClient } from "@prisma/client";
1110
const client = new PrismaClient();
1211

1312
app.get("/", (request, response) => {
14-
const index = fs.readFileSync("index.ejs", "utf-8");
15-
const html = ejs.render(index, {
16-
warning: "",
17-
});
13+
const index = fs.readFileSync("index.html", "utf-8");
14+
const html = index.replace("{warning}", "");
1815
response.send(html);
1916
});
2017

2118
app.post("/login", async (request, response) => {
22-
const login = fs.readFileSync("login.ejs", "utf-8");
23-
const index = fs.readFileSync("index.ejs", "utf-8");
19+
const login = fs.readFileSync("login.html", "utf-8");
20+
const index = fs.readFileSync("index.html", "utf-8");
2421
const user = await client.User.findUnique({
2522
where: {
2623
username: request.body.username,
2724
},
2825
});
2926
if (user === undefined) {
30-
const html = ejs.render(index, {
31-
warning: "入力されたユーザー名は存在しません。",
32-
});
27+
const html = index.replace("{warning}", "入力されたユーザー名は存在しません。");
3328
response.send(html);
3429
} else if (user.password === request.body.password) {
3530
const sessionId = await client.Session.findFirst({
@@ -41,57 +36,47 @@ app.post("/login", async (request, response) => {
4136
where: { id: sessionId.id },
4237
});
4338
response.cookie("session", sessionId.id);
44-
const html = ejs.render(login, {
45-
username: prof.name,
46-
});
39+
const html = login.replace("{username}", prof.username);
4740
response.send(html);
4841
} else {
49-
const html = ejs.render(index, {
50-
warning: "パスワードが違います。",
51-
});
42+
const html = index.replace("{warning}", "パスワードが違います。");
5243
response.send(html);
5344
}
5445
});
5546

5647
app.get("/profile", async (request, response) => {
57-
const profile = fs.readFileSync("profile.ejs", "utf-8");
48+
const profile = fs.readFileSync("profile.html", "utf-8");
5849
const prof = await client.Profile.findUnique({
5950
where: { id: request.cookies.session },
6051
});
61-
const html = ejs.render(profile, {
62-
name: prof.name,
63-
age: prof.age,
64-
univ: prof.univ,
65-
});
52+
const html = profile.replace("{name}", prof.name)
53+
.replace("{age}", prof.age)
54+
.replace("{univ}", prof.univ);
6655
response.send(html);
6756
});
6857

69-
app.get("/resister", (request, response) => {
70-
const resister = fs.readFileSync("resister.ejs", "utf-8");
71-
const html = ejs.render(resister, {
72-
warning: "",
73-
});
58+
app.get("/register", (request, response) => {
59+
const register = fs.readFileSync("register.html", "utf-8");
60+
const html = register.replace("{warning}", "");
7461
response.send(html);
7562
});
7663

77-
app.post("/resistered", async (request, response) => {
78-
const index = fs.readFileSync("index.ejs", "utf-8");
79-
const resister = fs.readFileSync("resister.ejs", "utf-8");
64+
app.post("/registered", async (request, response) => {
65+
const index = fs.readFileSync("index.html", "utf-8");
66+
const register = fs.readFileSync("register.html", "utf-8");
8067
const user = await client.User.findUnique({
8168
where: {
8269
username: request.body.username,
8370
},
8471
});
8572
if (
86-
(request.body.username === "") |
87-
(request.body.password === "") |
88-
(request.body.name === "") |
89-
(request.body.age === "") |
73+
(request.body.username === "") ||
74+
(request.body.password === "") ||
75+
(request.body.name === "") ||
76+
(request.body.age === "") ||
9077
(request.body.univ === "")
9178
) {
92-
const html = ejs.render(resister, {
93-
warning: "未記入の項目があります。",
94-
});
79+
const html = register.replace("{warning}", "未記入の項目があります。");
9580
response.send(html);
9681
} else if (user === undefined) {
9782
const new_user = await client.User.create({
@@ -114,15 +99,10 @@ app.post("/resistered", async (request, response) => {
11499
univ: request.body.univ,
115100
},
116101
});
117-
const html = ejs.render(index, {
118-
warning: "登録が完了しました。",
119-
});
102+
const html = index.replace("{warning}", "登録が完了しました。");
120103
response.send(html);
121104
} else {
122-
const html = ejs.render(resister, {
123-
warning:
124-
"入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。",
125-
});
105+
const html = register.replace("{warning}", "入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。")
126106
response.send(html);
127107
}
128108
});

docs/3-web-servers/08-cookie/_samples/profile/profile.ejs renamed to docs/3-web-servers/08-cookie/_samples/profile/profile.html

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<!doctype html>
2-
<html lang="ja">
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>profile</title>
6-
</head>
7-
<body>
8-
<li>氏名:<%= name %></li>
9-
<li>年齢:<%= age %></li>
10-
<li>大学:<%= univ %></li>
11-
</body>
12-
</html>
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>profile</title>
6+
</head>
7+
<body>
8+
<li>氏名:{name}</li>
9+
<li>年齢:{age}</li>
10+
<li>大学:{univ}</li>
11+
</body>
12+
</html>

docs/3-web-servers/08-cookie/_samples/profile/resister.ejs renamed to docs/3-web-servers/08-cookie/_samples/profile/register.html

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
<!doctype html>
2-
<html lang="ja">
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>profile</title>
6-
</head>
7-
<body>
8-
<form method="post" action="/resistered">
9-
<div>
10-
ユーザー名
11-
<input name="username" />
12-
</div>
13-
<div>
14-
パスワード
15-
<input name="password" type="password" />
16-
</div>
17-
<div>
18-
氏名
19-
<input name="name" />
20-
</div>
21-
<div>
22-
年齢
23-
<input name="age" />
24-
</div>
25-
<div>
26-
大学
27-
<input name="univ" />
28-
</div>
29-
<button>新規登録</button>
30-
</form>
31-
<p><%= warning %></p>
32-
</body>
33-
</html>
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>profile</title>
6+
</head>
7+
<body>
8+
<form method="post" action="/resistered">
9+
<div>
10+
ユーザー名
11+
<input name="username" />
12+
</div>
13+
<div>
14+
パスワード
15+
<input name="password" type="password" />
16+
</div>
17+
<div>
18+
氏名
19+
<input name="name" />
20+
</div>
21+
<div>
22+
年齢
23+
<input name="age" />
24+
</div>
25+
<div>
26+
大学
27+
<input name="univ" />
28+
</div>
29+
<button>新規登録</button>
30+
</form>
31+
<p><%= warning %></p>
32+
</body>
33+
</html>

0 commit comments

Comments
 (0)