Skip to content

Commit 7e9519c

Browse files
authored
Merge pull request #583 from ut-code/migrate-from-template-literal
テンプレートリテラルからの移行
2 parents f65c8cc + 3808c8c commit 7e9519c

File tree

7 files changed

+74
-61
lines changed

7 files changed

+74
-61
lines changed
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import express from "express";
2+
import { readFileSync } from "fs";
23

34
const app = express();
45

@@ -16,22 +17,12 @@ app.get("/send", (request, response) => {
1617
const selectedBooks = books.filter(
1718
(book) => book.author === request.query.author,
1819
);
19-
response.send(`
20-
<!doctype html>
21-
<html lang="ja">
22-
<head>
23-
<meta charset="UTF-8" />
24-
<title>Document</title>
25-
</head>
26-
<body>
27-
<ul>
28-
${selectedBooks
29-
.map((selectedBook) => `<li>${selectedBook.title}</li>`)
30-
.join("")}
31-
</ul>
32-
</body>
33-
</html>
34-
`);
20+
const template = readFileSync("./send.html", "utf-8");
21+
const html = template.replace(
22+
"{books}",
23+
selectedBooks.map((book) => `<li>${book.title}</li>`).join(""),
24+
);
25+
response.send(html);
3526
});
3627

3728
app.listen(3000);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
<ul>
9+
{books}
10+
</ul>
11+
</body>
12+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
<ul>
9+
{messages}
10+
</ul>
11+
<form action="/send" method="post">
12+
<input name="message" />
13+
<button>送信</button>
14+
</form>
15+
</body>
16+
</html>
Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import express from "express";
2+
import { readFileSync } from "fs";
23

34
const app = express();
45

@@ -7,29 +8,17 @@ const messages = [];
78
app.use(express.urlencoded({ extended: true }));
89

910
app.get("/", (request, response) => {
10-
response.send(`
11-
<!doctype html>
12-
<html lang="ja">
13-
<head>
14-
<meta charset="UTF-8" />
15-
<title>Document</title>
16-
</head>
17-
<body>
18-
<ul>
19-
${messages.map((message) => `<li>${message}</li>`).join("")}
20-
</ul>
21-
<form action="/send" method="post">
22-
<input name="message" />
23-
<button>送信</button>
24-
</form>
25-
</body>
26-
</html>
27-
`);
11+
const template = readFileSync("./index.html", "utf-8");
12+
const html = template.replace(
13+
"{messages}",
14+
messages.map((message) => `<li>${message}</li>`).join(""),
15+
);
16+
response.send(html);
2817
});
2918

3019
app.post("/send", (request, response) => {
3120
messages.push(request.body.message);
32-
response.send("送信しました");
21+
response.send("送信しました");
3322
});
3423

3524
app.listen(3000);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="ja">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<ul>
9+
{messages}
10+
</ul>
11+
<form method="post" action="/send">
12+
<input placeholder="ここに入力してください。" name="message" />
13+
<button>送信</button>
14+
</form>
15+
</body>
16+
</html>

docs/3-web-servers/07-database/_samples/forum/main.mjs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from "express";
22
import { PrismaClient } from "@prisma/client";
3+
import { readFileSync } from "fs";
34

45
const app = express();
56

@@ -11,24 +12,12 @@ app.get("/", async (request, response) => {
1112
const messages = await (
1213
await client.forum.findMany()
1314
).map((data) => data.message);
14-
response.send(`
15-
<!doctype html>
16-
<html lang="ja">
17-
<head>
18-
<meta charset="utf-8" />
19-
<title>Title</title>
20-
</head>
21-
<body>
22-
<ul>
23-
${messages.map((message) => `<li>${message}</li>`).join("")}
24-
</ul>
25-
<form method="post" action="/send">
26-
<input placeholder="ここに入力してください。" name="message" />
27-
<button>送信</button>
28-
</form>
29-
</body>
30-
</html>
31-
`);
15+
const template = readFileSync("./index.html", "utf-8");
16+
const html = template.replace(
17+
"{messages}",
18+
messages.map((message) => `<li>${message}</li>`).join(""),
19+
);
20+
response.send(html);
3221
});
3322

3423
app.post("/send", async (request, response) => {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express";
22
import cookieParser from "cookie-parser";
3-
import fs from "fs";
3+
import { readFileSync } from "fs";
44

55
const app = express();
66
app.use(express.urlencoded({ extended: true }));
@@ -10,14 +10,14 @@ import { PrismaClient } from "@prisma/client";
1010
const client = new PrismaClient();
1111

1212
app.get("/", (request, response) => {
13-
const index = fs.readFileSync("index.html", "utf-8");
13+
const index = readFileSync("index.html", "utf-8");
1414
const html = index.replace("{warning}", "");
1515
response.send(html);
1616
});
1717

1818
app.post("/login", async (request, response) => {
19-
const login = fs.readFileSync("login.html", "utf-8");
20-
const index = fs.readFileSync("index.html", "utf-8");
19+
const login = readFileSync("login.html", "utf-8");
20+
const index = readFileSync("index.html", "utf-8");
2121
const user = await client.User.findUnique({
2222
where: {
2323
username: request.body.username,
@@ -48,7 +48,7 @@ app.post("/login", async (request, response) => {
4848
});
4949

5050
app.get("/profile", async (request, response) => {
51-
const profile = fs.readFileSync("profile.html", "utf-8");
51+
const profile = readFileSync("profile.html", "utf-8");
5252
const prof = await client.Profile.findUnique({
5353
where: { id: request.cookies.session },
5454
});
@@ -60,14 +60,14 @@ app.get("/profile", async (request, response) => {
6060
});
6161

6262
app.get("/register", (request, response) => {
63-
const register = fs.readFileSync("register.html", "utf-8");
63+
const register = readFileSync("register.html", "utf-8");
6464
const html = register.replace("{warning}", "");
6565
response.send(html);
6666
});
6767

6868
app.post("/registered", async (request, response) => {
69-
const index = fs.readFileSync("index.html", "utf-8");
70-
const register = fs.readFileSync("register.html", "utf-8");
69+
const index = readFileSync("index.html", "utf-8");
70+
const register = readFileSync("register.html", "utf-8");
7171
const user = await client.User.findUnique({
7272
where: {
7373
username: request.body.username,

0 commit comments

Comments
 (0)