* chore: rename APIs and Microservices to include "Backend" (#42515) * fix typo * fix typo * undo change * Corrected grammar mistake Corrected a grammar mistake by removing a comma. * change APIs and Microservices cert title * update title * Change APIs and Microservices certi title * Update translations.json * update title * feat(curriculum): rename apis and microservices cert * rename folder structure * rename certificate * rename learn Markdown * apis-and-microservices -> back-end-development-and-apis * update backend meta * update i18n langs and cypress test Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: add development to front-end libraries (#42512) * fix: added-the-word-Development-to-front-end-libraries * fix/added-the-word-Development-to-front-end-libraries * fix/added-word-development-to-front-end-libraries-in-other-related-files * fix/added-the-word-Development-to-front-end-and-all-related-files * fix/removed-typos-from-last-commit-in-index.md * fix/reverted-changes-that-i-made-to-dependecies * fix/removed xvfg * fix/reverted changes that i made to package.json * remove unwanted changes * front-end-development-libraries changes * rename backend certSlug and README * update i18n folder names and keys * test: add legacy path redirect tests This uses serve.json from the client-config repo, since we currently use that in production * fix: create public dir before moving serve.json * fix: add missing script * refactor: collect redirect tests * test: convert to cy.location for stricter tests * rename certificate folder to 00-certificates * change crowdin config to recognise new certificates location * allow translations to be used Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com> * add forwards slashes to path redirects * fix cypress path tests again * plese cypress * fix: test different challenge Okay so I literally have no idea why this one particular challenge fails in Cypress Firefox ONLY. Tom and I paired and spun a full build instance and confirmed in Firefox the page loads and redirects as expected. Changing to another bootstrap challenge passes Cypress firefox locally. Absolutely boggled by this. AAAAAAAAAAAAAAA * fix: separate the test Okay apparently the test does not work unless we separate it into a different `it` statement. >:( >:( >:( >:( Co-authored-by: Sujal Gupta <55016909+heysujal@users.noreply.github.com> Co-authored-by: Noor Fakhry <65724923+NoorFakhry@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
64 lines
2.9 KiB
Markdown
64 lines
2.9 KiB
Markdown
---
|
||
id: 587d7fb2367417b2b2512bf7
|
||
title: 使用 body-parser 來解析 POST 請求
|
||
challengeType: 2
|
||
forumTopicId: 301520
|
||
dashedName: use-body-parser-to-parse-post-requests
|
||
---
|
||
|
||
# --description--
|
||
|
||
除了 GET 還有另一個常見的 HTTP 動詞,即 POST。 POST 是使用 HTML 表單發送客戶端數據的默認方法。 在 REST 規範中,POST 常用於發送數據以在數據庫中創建新項目(新用戶或新博客文章)。 在這個項目中沒有使用數據庫,但下面將學習如何處理 POST 請求。
|
||
|
||
在這些類型的請求中,數據不會出現在 URL 中,而是隱藏在請求正文中。 請求正文也是 HTML 請求的一部分,被稱爲負載。 即使數據在 URL 中是不可見的,也不意味着它是私有的。 要了解原因,請觀察 HTTP POST 請求的原始內容:
|
||
|
||
```http
|
||
POST /path/subpath HTTP/1.0
|
||
From: john@example.com
|
||
User-Agent: someBrowser/1.0
|
||
Content-Type: application/x-www-form-urlencoded
|
||
Content-Length: 20
|
||
|
||
name=John+Doe&age=25
|
||
```
|
||
|
||
正如你所看到的,正文被編碼成類似查詢字符串的形式, 這是 HTML 表單使用的默認格式。 我們還可以通過 Ajax 使用 JSON 來處理具有更復雜結構的數據。 還有另一種類型的編碼:multipart/form-data, 它被用來上傳二進制文件。 在本練習中,我們將使用 URL 編碼請求正文。 要解析來自 POST 請求的數據,你必須安裝 `body-parser` 包, 這個包包含一套可以解碼不同格式數據的中間件。
|
||
|
||
# --instructions--
|
||
|
||
在 `package.json` 中安裝 `body-parser` 模塊, 然後在文件頂部 `require` 進來, 用變量 `bodyParser` 保存它。 通過中間件的 `bodyParser.urlencoded({extended: false})` 方法處理 URL 編碼數據, 將通過先前的方法調用返回的函數傳遞到 `app.use()`。 中間件通常掛載在所有需要它的路由之前。
|
||
|
||
**注意:** `extended` 是一個配置選項, 告訴 `body-parser` 需要使用哪個解析。 當 `extended=false` 時,它使用經典編碼 `querystring` 庫。 當 `extended=true`時,它使用 `qs` 庫進行解析。
|
||
|
||
當使用 `extended=false` 時,值可以只是字符串或數組。 使用 `querystring` 時返回的對象並不繼承的 JavaScript `Object`,這意味着 `hasOwnProperty`、`toString` 等函數將不可用。 拓展版本的數據更加靈活,但稍遜於 JSON。
|
||
|
||
# --hints--
|
||
|
||
應該掛載 “body-parser” 中間件
|
||
|
||
```js
|
||
(getUserInput) =>
|
||
$.get(getUserInput('url') + '/_api/add-body-parser').then(
|
||
(data) => {
|
||
assert.isAbove(
|
||
data.mountedAt,
|
||
0,
|
||
'"body-parser" is not mounted correctly'
|
||
);
|
||
},
|
||
(xhr) => {
|
||
throw new Error(xhr.responseText);
|
||
}
|
||
);
|
||
```
|
||
|
||
# --solutions--
|
||
|
||
```js
|
||
/**
|
||
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.
|
||
*/
|
||
```
|