Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/3-web-servers/05-server/_samples/app-use/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { readFileSync } from "node:fs";

const app = express();
app.use((request, response) => {
response.send(readFileSync("./static" + request.path, "utf-8"));
response.send(readFileSync("./public" + request.path, "utf-8"));
});
app.listen(3000);
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { readFileSync } from "node:fs";
const app = express();

app.get("/", (request, response) => {
response.send(readFileSync("static/index.html", "utf-8"));
response.send(readFileSync("./public/index.html", "utf-8"));
});
app.get("/script.js", (request, response) => {
response.send(readFileSync("static/script.js", "utf-8"));
response.send(readFileSync("./public/script.js", "utf-8"));
});
app.get("/sub/", (request, response) => {
response.send(readFileSync("static/sub/index.html", "utf-8"));
response.send(readFileSync("./public/sub/index.html", "utf-8"));
});
app.get("/sub/script.js", (request, response) => {
response.send(readFileSync("static/sub/script.js", "utf-8"));
response.send(readFileSync("./public/sub/script.js", "utf-8"));
});

app.listen(3000);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express";

const app = express();
app.use(express.static("static"));
app.use(express.static("./public"));
app.listen(3000);
16 changes: 8 additions & 8 deletions docs/3-web-servers/05-server/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ import { readFileSync } from "node:fs";
const app = express();

app.get("/", (request, response) => {
response.send(readFileSync("./static/index.html", "utf-8"));
response.send(readFileSync("./public/index.html", "utf-8"));
});
app.get("/script.js", (request, response) => {
response.send(readFileSync("./static/script.js", "utf-8"));
response.send(readFileSync("./public/script.js", "utf-8"));
});
app.get("/sub/", (request, response) => {
response.send(readFileSync("./static/sub/index.html", "utf-8"));
response.send(readFileSync("./public/sub/index.html", "utf-8"));
});
app.get("/sub/script.js", (request, response) => {
response.send(readFileSync("./static/sub/script.js", "utf-8"));
response.send(readFileSync("./public/sub/script.js", "utf-8"));
});

app.listen(3000);
Expand All @@ -231,17 +231,17 @@ app.listen(3000);
import express from "express";

const app = express();
app.use(express.static("static"));
app.use(express.static("./public"));
app.listen(3000);
```

<ViewSource url={import.meta.url} path="_samples/static-hosting-smart" />

これにより、リクエストのパスをもとに、`static`フォルダ内の適切なファイルが自動的に配信されます。
これにより、リクエストのパスをもとに、`public`フォルダ内の適切なファイルが自動的に配信されます。

:::tip[`index.html`の省略]

`express.static`を用いる場合、`index.html`は省略可能になります。つまり、`/`へのリクエストで`static/index.html`が、`/sub`へのリクエストで`static/sub/index.html`にアクセスできるようになります。これは、ExpressやJavaScriptに限ったことではなく、多くのWebサーバーの実装において、こういったルールが成り立ちます。
`express.static`を用いる場合、`index.html`は省略可能になります。つまり、`/`へのリクエストで`public/index.html`が、`/sub`へのリクエストで`public/sub/index.html`にアクセスできるようになります。これは、ExpressやJavaScriptに限ったことではなく、多くのWebサーバーの実装において、こういったルールが成り立ちます。

:::

Expand All @@ -259,7 +259,7 @@ Expressの [`Request`](https://expressjs.com/ja/api.html#req) オブジェクト
```javascript
const app = express();
app.use((request, response) => {
response.send(readFileSync("./static" + request.path, "utf-8"));
response.send(readFileSync("./public" + request.path, "utf-8"));
});
app.listen(3000);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const books = [
{ title: "高瀬舟", author: "森鴎外" },
];

app.use(express.static("static"));
app.use(express.static("./public"));

app.get("/send", (request, response) => {
const selectedBooks = books.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
import { emojify } from "node-emoji";

const app = express();
app.use(express.static("static"));
app.use(express.static("./public"));

app.get("/emojify", (request, response) => {
const text = request.query.text;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from "express";

const app = express();
app.use(express.static("static"));
app.use(express.static("./public"));
app.get("/send", (request, response) => {
response.send(
`あなたの名前は${request.query.name}で、${request.query.age}歳ですね。`,
Expand Down
12 changes: 6 additions & 6 deletions docs/3-web-servers/06-form/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ encodeURIComponent("日本語"); // "%E6%97%A5%E6%9C%AC%E8%AA%9E"

以下のコードの、HTMLファイルと、JavaScriptファイルを作成して実行してみましょう。

```html title="static/index.html"
```html title="public/index.html"
<!doctype html>
<html lang="ja">
<head>
Expand All @@ -92,7 +92,7 @@ encodeURIComponent("日本語"); // "%E6%97%A5%E6%9C%AC%E8%AA%9E"
import express from "express";

const app = express();
app.use(express.static("static"));
app.use(express.static("./public"));
app.get("/send", (request, response) => {
response.send(
`あなたの名前は${request.query.name}で、${request.query.age}歳ですね。`,
Expand Down Expand Up @@ -131,7 +131,7 @@ app.listen(3000);

`node-emoji`と`express`をインストールするのを忘れないようにしましょう。

```html title="static/index.html"
```html title="public/index.html"
<!doctype html>
<html lang="ja">
<head>
Expand All @@ -152,7 +152,7 @@ import express from "express";
import { emojify } from "node-emoji";

const app = express();
app.use(express.static("static"));
app.use(express.static("./public"));

app.get("/emojify", (request, response) => {
const text = request.query.text;
Expand Down Expand Up @@ -196,7 +196,7 @@ const evenNumbers = numbers.filter((number) => number % 2 === 0);

<Answer>

```html title="static/index.html"
```html title="public/index.html"
<form action="/send">
<input name="author" />
<button>検索</button>
Expand All @@ -216,7 +216,7 @@ const books = [
{ title: "高瀬舟", author: "森鴎外" },
];

app.use(express.static("static"));
app.use(express.static("./public"));

app.get("/send", (request, response) => {
const selectedBooks = books.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express from "express";
const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.static("static"));
app.use(express.static("./public"));

app.post("/send", (request, response) => {
response.send(
Expand Down
4 changes: 2 additions & 2 deletions docs/3-web-servers/07-get-post/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ HTTPリクエストのこのような区分を、<Term>**HTTPメソッド**</Ter

前頁の例を、POSTリクエストを用いて書き直してみましょう。`form`要素の`method`属性に`post`を指定することで、ブラウザは送信ボタンが押されたときに`POST`メソッドの<Term>リクエスト</Term>を発行します。

```html title="static/index.html"
```html title="public/index.html"
<!doctype html>
<html lang="ja">
<head>
Expand All @@ -43,7 +43,7 @@ import express from "express";
const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.static("static"));
app.use(express.static("./public"));

app.post("/send", (request, response) => {
response.send(
Expand Down
2 changes: 1 addition & 1 deletion docs/4-advanced/01-fetch-api/_samples/chat/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
const app = express();

app.use(express.json());
app.use(express.static("static"));
app.use(express.static("./public"));

const messages = [];

Expand Down
2 changes: 1 addition & 1 deletion docs/4-advanced/01-fetch-api/_samples/fetch-api/main.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from "express";
const app = express();

app.use(express.static("static"));
app.use(express.static("./public"));

app.get("/weather", (request, response) => {
response.send("晴れ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
const app = express();

app.use(express.json());
app.use(express.static("static"));
app.use(express.static("./public"));

app.post("/send", (request, response) => {
response.send(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.static("static"));
app.use(express.static("./public"));

app.post("/send", (request, response) => {
response.send(
Expand Down
20 changes: 10 additions & 10 deletions docs/4-advanced/01-fetch-api/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import chatAppVideo from "./chat-app.mp4";
{/* prettier-ignore */}
<Term>サーバー</Term>と<Term>クライアント</Term>、どちらで動くJavaScriptなのかに注意しながら、次のプログラムを実行してみましょう。

```html title="static/index.htmlのbody内"
```html title="public/index.htmlのbody内"
<button id="fetch-button">天気予報を見る</button>
```

```javascript title="static/script.js (ブラウザ上で動くJavaScript)"
```javascript title="public/script.js (ブラウザ上で動くJavaScript)"
document.getElementById("fetch-button").onclick = async () => {
const response = await fetch("/weather");
const weather = await response.text();
Expand All @@ -39,7 +39,7 @@ document.getElementById("fetch-button").onclick = async () => {
import express from "express";
const app = express();

app.use(express.static("static"));
app.use(express.static("./public"));

app.get("/weather", (request, response) => {
response.send("晴れ");
Expand All @@ -54,7 +54,7 @@ app.listen(3000);

このとき、<Term>リクエストボディ</Term>は、`fetch`関数の第2引数に指定したオブジェクトの`body`プロパティに指定します。

```javascript title="static/script.js"
```javascript title="public/script.js"
document.getElementById("send-button").onclick = async () => {
const name = document.getElementById("name").value;
const age = document.getElementById("age").value;
Expand All @@ -70,7 +70,7 @@ import express from "express";
const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.static("static"));
app.use(express.static("./public"));

app.post("/send", (request, response) => {
response.send(
Expand All @@ -93,7 +93,7 @@ HTMLのフォームで送ったものと同じ形式でデータを送信する

`fetch`関数の第2引数の`headers`オプションでは、<Term>リクエストヘッダ</Term>を指定します。<Term>リクエストボディ</Term>に<Term>JSON</Term>を指定する場合は、**`Content-Type`リクエストヘッダ**を`"application/json"`に指定します。

```javascript title="static/script.js"
```javascript title="public/script.js"
document.getElementById("send-button").onclick = async () => {
const name = document.getElementById("name").value;
const age = document.getElementById("age").value;
Expand All @@ -115,7 +115,7 @@ import express from "express";
const app = express();

app.use(express.json());
app.use(express.static("static"));
app.use(express.static("./public"));

app.post("/send", (request, response) => {
response.send(
Expand Down Expand Up @@ -174,7 +174,7 @@ app.get("/messages", (request, response) => {

新着メッセージを確認するために、定期的に`/messages`に対して`fetch`関数を用いて<Term>リクエスト</Term>しましょう。`setInterval`関数が利用できます。

```javascript title="static/script.js"
```javascript title="public/script.js"
setInterval(async () => {
const response = await fetch("/messages");
// レスポンスを処理する
Expand All @@ -183,11 +183,11 @@ setInterval(async () => {

`innerHTML`プロパティを空文字列とすることで要素の子要素を全て削除できます。`document.createElement`関数を用いて再び生成し直しましょう。

```html title="static/index.html"
```html title="public/index.html"
<ul id="message-list"></ul>
```

```javascript title="static/script.js"
```javascript title="public/script.js"
const messageList = document.getElementById("message-list");
messageList.innerHTML = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const app = express();
app.use(express.json());

// Vite によって出力されたディレクトリを配信する
app.use(express.static("dist"));
app.use(express.static("./dist"));

const messages = [];

Expand Down
2 changes: 1 addition & 1 deletion docs/4-advanced/02-bundler/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const app = express();
app.use(express.json());

// Viteによって出力されたディレクトリを配信する
app.use(express.static("dist"));
app.use(express.static("./dist"));

app.listen(3000);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const app = express();
const { PrismaClient } = require("@prisma/client");
const client = new PrismaClient();

app.use(express.static("dist"));
app.use(express.static("./dist"));
app.use(express.json());

app.get("/todos", async (request, response) => {
Expand Down