* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.6 KiB
2.6 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7fb2367417b2b2512bf7 | 使用 body-parser 来解析POST请求 | 2 | 301520 | use-body-parser-to-parse-post-requests |
--description--
除了 GET 还有另一个常见的 http 动词,它是 POST。POST 是使用 HTML 表单发送客户端数据的默认方法。在 REST 规范中,POST 常用于发送数据,以便在数据库中创建新项目(新用户或新博客文章)。我们在这个项目中没有使用数据库,我们将学习如何处理 POST 请求。
在这些类型的请求中,数据不会出现在 URL 中,而是隐藏在请求正文中。这也是 HTML 请求的一部分,被称为负载。因为 HTML 是基于文本的,你看不到数据,这并不意味着它们是加密的。HTTP POST 请求的原始内容如下所示:
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。它用来上传二进制文件。 在本练习中,我们将使用网址编码 body。要解析来自 POST 请求的数据,你必须安装一个包:body-parser。这个包允许你使用一套可以解码不同格式数据的中间件,在这里查看文档。
--instructions--
在 package.json 中安装 body-parser 模块,然后在文件顶部 require 进来,用变量 bodyParser 保存它。
处理 URL 编码数据通过中间件的bodyParser.urlencoded({extended: false})
方法。extended=false
是一个配置选项,告诉解析器使用经典编码。当你使用它时,值只能是字符串或者数组。继承版使用起来数据更加灵活,它比 JSON 更好。传递给app.use()
上一次方法调用返回的函数。通常中间件必须挂载在所有需要它的路由之前。
--hints--
"body-parser" 中间件应该被挂载
(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--
/**
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.
*/