Files
2022-01-20 20:30:18 +01:00

3.1 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7fb2367417b2b2512bf6 クライアントからクエリパラメーター入力を取得する 2 301512 get-query-parameter-input-from-the-client

--description--

クライアントから入力を取得するもう一つの一般的な方法として、クエリ文字列を使用してルートパスの後にデータをエンコードすることができます。 クエリ文字列は、疑問符 (?) で区切られ、field=value のペアを含みます。 各ペアはアンパサンド (&) で区切られます。 Express では、クエリ文字列のデータを解析し、req.query オブジェクトを設定することができます。 パーセント (%) などのいくつかの文字は URL に含めることができないので、送信する前に別の形式にエンコードする必要があります。 JavaScript の API を使用する場合は、そうした文字をエンコード/デコードするために特定のメソッドを使用できます。

route_path: '/library'
actual_request_URL: '/library?userId=546&bookId=6754'
req.query: {userId: '546', bookId: '6754'}

--instructions--

API エンドポイントを構築し、GET /name でマウントしてください。 JSON ドキュメントで応答し、構造体 { name: 'firstname lastname'} を受け取ってください。 ファーストネームおよびラストネームのパラメーターは、?first=firstname&last=lastname などのクエリ文字列にエンコードする必要があります。

** 注: ** 以下の演習では、同じ /name ルートパスで、POST リクエストからデータを受け取ります。 必要に応じて、メソッド app.route(path).get(handler).post(handler) を使用できます。 このシンタックスにより、同じパスルート上で異なる動詞ハンドラーをチェーンすることができます。 入力する文字数が少しだけ減り、コードがわかりやすくなります。

--hints--

テスト 1: API エンドポイントは、正しい名前で応答する必要があります。

(getUserInput) =>
  $.get(getUserInput('url') + '/name?first=Mick&last=Jagger').then(
    (data) => {
      assert.equal(
        data.name,
        'Mick Jagger',
        'Test 1: "GET /name" route does not behave as expected'
      );
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

テスト 2: API エンドポイントは、正しい名前で応答する必要があります。

(getUserInput) =>
  $.get(getUserInput('url') + '/name?last=Richards&first=Keith').then(
    (data) => {
      assert.equal(
        data.name,
        'Keith Richards',
        'Test 2: "GET /name" route does not behave as expected'
      );
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

--solutions--

/**
  Backend challenges don't need solutions, 
  because they would need to be tested against a full working project. 
  Please check our contributing guidelines to learn more.
*/