Skip to content

Commit f65c8cc

Browse files
authored
Merge pull request #581 from ut-code/migrate-from-ejs
EJSからの移行
2 parents af5e57a + 76a876c commit f65c8cc

File tree

5 files changed

+104
-117
lines changed

5 files changed

+104
-117
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="/register">新規登録</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: 32 additions & 45 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,24 @@ 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(
28+
"{warning}",
29+
"入力されたユーザー名は存在しません。",
30+
);
3331
response.send(html);
3432
} else if (user.password === request.body.password) {
3533
const sessionId = await client.Session.findFirst({
@@ -41,57 +39,48 @@ app.post("/login", async (request, response) => {
4139
where: { id: sessionId.id },
4240
});
4341
response.cookie("session", sessionId.id);
44-
const html = ejs.render(login, {
45-
username: prof.name,
46-
});
42+
const html = login.replace("{username}", prof.username);
4743
response.send(html);
4844
} else {
49-
const html = ejs.render(index, {
50-
warning: "パスワードが違います。",
51-
});
45+
const html = index.replace("{warning}", "パスワードが違います。");
5246
response.send(html);
5347
}
5448
});
5549

5650
app.get("/profile", async (request, response) => {
57-
const profile = fs.readFileSync("profile.ejs", "utf-8");
51+
const profile = fs.readFileSync("profile.html", "utf-8");
5852
const prof = await client.Profile.findUnique({
5953
where: { id: request.cookies.session },
6054
});
61-
const html = ejs.render(profile, {
62-
name: prof.name,
63-
age: prof.age,
64-
univ: prof.univ,
65-
});
55+
const html = profile
56+
.replace("{name}", prof.name)
57+
.replace("{age}", prof.age)
58+
.replace("{univ}", prof.univ);
6659
response.send(html);
6760
});
6861

69-
app.get("/resister", (request, response) => {
70-
const resister = fs.readFileSync("resister.ejs", "utf-8");
71-
const html = ejs.render(resister, {
72-
warning: "",
73-
});
62+
app.get("/register", (request, response) => {
63+
const register = fs.readFileSync("register.html", "utf-8");
64+
const html = register.replace("{warning}", "");
7465
response.send(html);
7566
});
7667

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");
68+
app.post("/registered", async (request, response) => {
69+
const index = fs.readFileSync("index.html", "utf-8");
70+
const register = fs.readFileSync("register.html", "utf-8");
8071
const user = await client.User.findUnique({
8172
where: {
8273
username: request.body.username,
8374
},
8475
});
8576
if (
86-
(request.body.username === "") |
87-
(request.body.password === "") |
88-
(request.body.name === "") |
89-
(request.body.age === "") |
90-
(request.body.univ === "")
77+
request.body.username === "" ||
78+
request.body.password === "" ||
79+
request.body.name === "" ||
80+
request.body.age === "" ||
81+
request.body.univ === ""
9182
) {
92-
const html = ejs.render(resister, {
93-
warning: "未記入の項目があります。",
94-
});
83+
const html = register.replace("{warning}", "未記入の項目があります。");
9584
response.send(html);
9685
} else if (user === undefined) {
9786
const new_user = await client.User.create({
@@ -114,15 +103,13 @@ app.post("/resistered", async (request, response) => {
114103
univ: request.body.univ,
115104
},
116105
});
117-
const html = ejs.render(index, {
118-
warning: "登録が完了しました。",
119-
});
106+
const html = index.replace("{warning}", "登録が完了しました。");
120107
response.send(html);
121108
} else {
122-
const html = ejs.render(resister, {
123-
warning:
124-
"入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。",
125-
});
109+
const html = register.replace(
110+
"{warning}",
111+
"入力されたユーザー名はすでに使用されています。別のユーザー名を入力してください。",
112+
);
126113
response.send(html);
127114
}
128115
});

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="/registered">
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)