File tree Expand file tree Collapse file tree 5 files changed +10
-4
lines changed
docs/3-web-servers/06-fetch-api Expand file tree Collapse file tree 5 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -2,10 +2,12 @@ import express from "express";
22
33const app = express ( ) ;
44app . use ( express . static ( "./public" ) ) ;
5+
56app . get ( "/weather" , ( request , response ) => {
67 response . json ( {
78 condition : "晴れ" ,
89 temperature : 25 ,
910 } ) ;
1011} ) ;
12+
1113app . listen ( 3000 ) ;
Original file line number Diff line number Diff line change @@ -5,4 +5,5 @@ async function fetchWeather() {
55 document . getElementById ( "condition" ) . textContent = weather . condition ;
66 document . getElementById ( "temperature" ) . textContent = weather . temperature ;
77}
8+
89fetchWeather ( ) ;
Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ import express from "express";
22
33const app = express ( ) ;
44app . use ( express . static ( "./public" ) ) ;
5+
56app . get ( "/weather" , ( request , response ) => {
67 response . send ( "晴れ" ) ;
78} ) ;
9+
810app . listen ( 3000 ) ;
Original file line number Diff line number Diff line change 11// await演算子を使用するため、asyncキーワードが必要
22async function fetchWeather ( ) {
33 // http://localhost:3000/weather にHTTPリクエストを送信
4- // fetch関数は非同期処理を行うため、awaitを演算子を適用して完了を待機する
4+ // fetch関数は非同期処理を行うため、await演算子を適用して完了を待機する
55 const response = await fetch ( "/weather" ) ;
66 // レスポンスをテキストとして取得
77 const weather = await response . text ( ) ;
88 document . getElementById ( "weather" ) . textContent = weather ;
99}
10+
1011fetchWeather ( ) ;
Original file line number Diff line number Diff line change @@ -10,11 +10,11 @@ title: Fetch APIによるデータの取得
1010
1111しかし、この方法では、新しい情報を取得する度にブラウザのページ遷移が必要になるため、複雑なアプリケーションを構築するには不便です。** Fetch API** を用いると、ブラウザ上で動作するJavaScriptから、Webサーバーに対して直接HTTPリクエストを発行し、レスポンスを受け取ることができます。これにより、Webアプリケーションはページ遷移を伴わずにサーバーと通信することが可能になります。
1212
13- ## Fetch API を使用する
13+ ## Fetch APIを使用する
1414
1515Fetch APIは、` fetch ` 関数を呼び出すことで実行できます。` fetch ` 関数にリクエストを発行したいURLを指定することで、HTTPリクエストを発行し、[ ` Response ` ] ( https://developer.mozilla.org/ja/docs/Web/API/Response ) オブジェクトを取得できます。` Response ` オブジェクトの[ ` text ` メソッド] ( https://developer.mozilla.org/ja/docs/Web/API/Response/text ) を呼び出すことで、<Term >レスポンス</Term >を文字列として取得できます。
1616
17- 次の例では、ブラウザは` script.mjs ` の中で` fetch("/weather") ` が呼び出されたときに、Webサーバーに対して` /weather ` というパスに対する<Term >リクエスト</Term >を発行します。Webサーバーは、` /weather ` に対するリクエストを受け取ると、レスポンスとして「晴れ」という文字列を返します。ブラウザは、このレスポンスを受け取り、HTMLの中の` span ` 要素に「晴れ」という文字列を表示します。
17+ 次の例では、ブラウザは` script.js ` の中で` fetch("/weather") ` が呼び出されたときに、Webサーバーに対して` /weather ` というパスに対する<Term >リクエスト</Term >を発行します。Webサーバーは、` /weather ` に対するリクエストを受け取ると、レスポンスとして「晴れ」という文字列を返します。ブラウザは、このレスポンスを受け取り、HTMLの中の` span ` 要素に「晴れ」という文字列を表示します。
1818
1919` fetch ` 関数や` Response#text ` メソッドは、時間のかかる処理を行うため、<Term >** 非同期処理** </Term >としてバックグラウンドで実行されます。戻り値に対して` await ` 演算子を適用することで、HTTPリクエストが完了するまで待機し、実際のレスポンスを取得できます。なお、` await ` 演算子を使用するためには、演算子が使用されている関数に` async ` キーワードを付与する必要があります。
2020
@@ -40,7 +40,7 @@ app.listen(3000);
4040// await演算子を使用するため、asyncキーワードが必要
4141async function fetchWeather () {
4242 // http://localhost:3000/weather にHTTPリクエストを送信
43- // fetch関数は非同期処理を行うため、awaitを演算子を適用して完了を待機する
43+ // fetch関数は非同期処理を行うため、await演算子を適用して完了を待機する
4444 const response = await fetch (" /weather" );
4545 // レスポンスをテキストとして取得
4646 const weather = await response .text ();
You can’t perform that action at this time.
0 commit comments