@@ -11,11 +11,11 @@ import chatAppVideo from "./chat-app.mp4";
1111しかしながら、ブラウザ上で動く JavaScript から利用できる ** Fetch <Term type =" api " >API</Term >** を用いると、任意のタイミングで<Term type =" httpRequestResponse " >リクエスト</Term >が発行できるようになります。<Term type =" api " >API</Term > は、アプリケーションプログラミングインターフェース(Application Programming Interface)の略で、あるソフトウェアの機能や管理するデータを、外部の他のソフトウェアで利用するための手順やデータ形式を定めた規約のことです。多くのソフトウェアが共通して利用する機能がまとめて提供されており、<Term type =" api " >API</Term > に従い短いコードを記述するだけでその機能を利用することができます。
1212<Term type =" serverClient " >サーバー</Term >と<Term type =" serverClient " >クライアント</Term >、どちらで動く JavaScript なのかに注意しながら、次のプログラムを実行してみましょう。
1313
14- ``` html title="/ static/index.html の body 内"
14+ ``` html title="static/index.html の body 内"
1515<button id =" fetch-button" >天気予報を見る</button >
1616```
1717
18- ``` javascript title="/ static/script.js (ブラウザ上で動く JavaScript)"
18+ ``` javascript title="static/script.js (ブラウザ上で動く JavaScript)"
1919document .getElementById (" fetch-button" ).onclick = async () => {
2020 const response = await fetch (" /weather" );
2121 const weather = await response .text ();
@@ -33,7 +33,7 @@ document.getElementById("fetch-button").onclick = async () => {
3333
3434なお、<Term type =" serverClient " >サーバー</Term >では次のプログラムが動作しているものとします。
3535
36- ``` javascript title="/ server.mjs (サーバーとして動く JavaScript)"
36+ ``` javascript title="server.mjs (サーバーとして動く JavaScript)"
3737import express from " express" ;
3838const app = express ();
3939
@@ -52,7 +52,7 @@ app.listen(3000);
5252
5353このとき、<Term type =" httpHeaderBody " >リクエストボディ</Term >は、 ` fetch ` 関数の第 2 引数に指定したオブジェクトの ` body ` プロパティに指定します。
5454
55- ``` javascript title="/ static/script.js"
55+ ``` javascript title="static/script.js"
5656document .getElementById (" send-button" ).onclick = async () => {
5757 const name = document .getElementById (" name" ).value ;
5858 const age = document .getElementById (" age" ).value ;
@@ -63,7 +63,7 @@ document.getElementById("send-button").onclick = async () => {
6363};
6464```
6565
66- ``` javascript title="/ server.mjs"
66+ ``` javascript title="server.mjs"
6767import express from " express" ;
6868const app = express ();
6969
@@ -91,7 +91,7 @@ HTML のフォームで送ったものと同じ形式でデータを送信する
9191
9292` fetch ` 関数の第 2 引数の ` headers ` オプションでは、<Term type =" httpHeaderBody " >リクエストヘッダ</Term >を指定します。<Term type =" httpHeaderBody " >リクエストボディ</Term >に <Term type =" json " >JSON</Term > を指定する場合は、** ` Content-Type ` リクエストヘッダ** を ` "application/json" ` に指定します。
9393
94- ``` javascript title="/ static/script.js"
94+ ``` javascript title="static/script.js"
9595document .getElementById (" send-button" ).onclick = async () => {
9696 const name = document .getElementById (" name" ).value ;
9797 const age = document .getElementById (" age" ).value ;
@@ -108,7 +108,7 @@ document.getElementById("send-button").onclick = async () => {
108108
109109サーバー側では、<Term type =" httpHeaderBody " >リクエストボディ</Term >の JSON を解釈するため、[ ` express.urlencoded ` ] ( https://expressjs.com/ja/api.html#express.urlencoded ) の代わりに [ ` express.json ` ] ( https://expressjs.com/ja/api.html#express.json ) を用います。
110110
111- ``` javascript title="/ server.mjs"
111+ ``` javascript title="server.mjs"
112112import express from " express" ;
113113const app = express ();
114114
@@ -153,7 +153,7 @@ app.listen(3000);
153153
154154[ 掲示板を作ったとき] ( ../../3-web-servers/06-get-post/index.md ) と同じく、` messages ` という配列をサーバー側に用意し、メッセージが送信されたらその配列に要素を追加するようにしましょう。
155155
156- ``` javascript title="/ server.js "
156+ ``` javascript title="server.mjs "
157157const messages = [];
158158app .post (" /send" , (request , response ) => {
159159 // メッセージを追加
@@ -164,15 +164,15 @@ app.post("/send", (request, response) => {
164164
165165[ ` express.Response#json ` メソッド] ( https://expressjs.com/ja/api.html#res.json ) は、受け取ったオブジェクトを ` JSON.stringify ` によって <Term type =" json " >JSON</Term > としたうえで<Term type =" httpRequestResponse " >レスポンス</Term >するためのメソッドです。このとき、` Content-Type ` レスポンスヘッダは自動的に ` "application/json" ` に設定されます。
166166
167- ``` javascript title="/ server.js "
167+ ``` javascript title="server.mjs "
168168app .get (" /messages" , (request , response ) => {
169169 response .json (messages);
170170});
171171```
172172
173173新着メッセージを確認するために、定期的に ` /messages ` に対して ` fetch ` 関数を用いて<Term type =" httpRequestResponse " >リクエスト</Term >しましょう。` setInterval ` 関数が利用できます。
174174
175- ``` javascript title="/ static/script.js"
175+ ``` javascript title="static/script.js"
176176setInterval (async () => {
177177 const response = await fetch (" /messages" );
178178 // レスポンスを処理する
@@ -181,11 +181,11 @@ setInterval(async () => {
181181
182182` innerHTML ` プロパティを空文字列とすることで要素の子要素を全て削除できます。` document.createElement ` 関数を用いて再び生成し直しましょう。
183183
184- ``` html title="/ static/index.html"
184+ ``` html title="static/index.html"
185185<ul id =" message-list" ></ul >
186186```
187187
188- ``` javascript title="/ static/script.js"
188+ ``` javascript title="static/script.js"
189189const messageList = document .getElementById (" message-list" );
190190messageList .innerHTML = " " ;
191191
0 commit comments