@@ -117,7 +117,7 @@ app.listen(3000);
117117このページの URL を見てみましょう。特に、クエリパラメータの部分に注目してみましょう。
118118![ URLのクエリパラメータ] ( form-url.png )
119119
120- ## 課題1
120+ ## 初級課題
121121
122122ログイン画面を作ってみましょう。以下のような画面を作成してください。
123123
@@ -139,11 +139,53 @@ const userCredentials = [
139139[ ` Array#find ` メソッド] ( https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/find ) を使用して、ユーザー名とパスワードが一致する要素を探してみましょう。
140140:::
141141
142- ### 解答例
142+ <Answer title = " ログイン画面" >
143+
144+ ``` html title="static/index.html"
145+ <form action =" /login" method =" post" >
146+ <input type =" text" name =" username" placeholder =" ユーザー名" />
147+ <input type =" password" name =" password" placeholder =" パスワード" />
148+ <button >ログイン</button >
149+ </form >
150+ ```
151+
152+ ``` javascript title="main.mjs"
153+ import express from " express" ;
154+ const app = express ();
155+
156+ app .use (express .static (" static" ));
157+ app .use (express .urlencoded ({ extended: true }));
158+
159+ const userCredentials = [
160+ { username: " tanaka" , password: " kfae12F@" },
161+ { username: " sato" , password: " faewbnpE3b=" },
162+ { username: " endo" , password: " fEnEoB131b" },
163+ ];
164+
165+ app .post (" /login" , (request , response ) => {
166+ const username = request .body .username ;
167+ const password = request .body .password ;
168+
169+ const userCredential = userCredentials .find (
170+ (userCredential ) =>
171+ userCredential .username === username &&
172+ userCredential .password === password,
173+ );
174+ if (userCredential === undefined ) {
175+ response .send (" ログインに失敗しました。" );
176+ } else {
177+ response .send (" ログインに成功しました。" );
178+ }
179+ });
180+
181+ app .listen (3000 );
182+ ```
143183
144184<ViewSource url = { import .meta .url } path = " _samples/login" />
145185
146- ## 課題2
186+ </Answer >
187+
188+ ## 中級課題
147189
148190書籍検索システムを作ってみましょう。まずは、配列に本のデータを登録します。
149191
@@ -172,8 +214,46 @@ const evenNumbers = numbers.filter((number) => number % 2 === 0);
172214
173215:::
174216
175- ### 解答例
217+ < Answer >
176218
177- 解答例は以下を参照してください
219+ ``` html title="static/index.html"
220+ <form action =" /send" >
221+ <input name =" author" />
222+ <button >送信</button >
223+ </form >
224+ ```
225+
226+ ``` javascript title="main.mjs"
227+ import express from " express" ;
228+ import { readFileSync } from " node:fs" ;
229+
230+ const app = express ();
231+
232+ const books = [
233+ { title: " 吾輩は猫である" , author: " 夏目漱石" },
234+ { title: " こころ" , author: " 夏目漱石" },
235+ { title: " 坊つちやん" , author: " 夏目漱石" },
236+ { title: " 舞姫" , author: " 森鴎外" },
237+ { title: " 高瀬舟" , author: " 森鴎外" },
238+ ];
239+
240+ app .use (express .static (" static" ));
241+
242+ app .get (" /send" , (request , response ) => {
243+ const selectedBooks = books .filter (
244+ (book ) => book .author === request .query .author ,
245+ );
246+ const template = readFileSync (" ./send.html" , " utf-8" );
247+ const html = template .replace (
248+ " <!-- books -->" ,
249+ selectedBooks .map ((book ) => ` <li>${ book .title } </li>` ).join (" " ),
250+ );
251+ response .send (html);
252+ });
253+
254+ app .listen (3000 );
255+ ```
178256
179257<ViewSource url = { import .meta .url } path = " _samples/book-search-system" />
258+
259+ </Answer >
0 commit comments