2019-05-06 11:31:26 +00:00
|
|
|
---
|
|
|
|
id: 587d7fb0367417b2b2512bef
|
2021-02-16 21:01:20 +00:00
|
|
|
title: 提供 HTML 文件服务
|
2019-05-06 11:31:26 +00:00
|
|
|
challengeType: 2
|
2020-09-07 05:40:31 +00:00
|
|
|
forumTopicId: 301516
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: serve-an-html-file
|
2019-05-06 11:31:26 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-02-16 21:01:20 +00:00
|
|
|
通过 `res.sendFile(path)` 方法给请求响应一个文件, 可以把它放到路由处理 `app.get('/', ...)` 中。 在后台,这个方法会根据你想发送的文件的类型,设置适当的消息头信息来告诉浏览器如何处理它, 然后读取并发送文件, 此方法需要文件的绝对路径。 建议使用 Node. js 的全局变量 `__dirname` 来计算出这个文件的绝对路径:
|
2019-10-14 19:29:55 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
absolutePath = __dirname + relativePath/file.ext
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2019-05-06 11:31:26 +00:00
|
|
|
|
2021-02-16 21:01:20 +00:00
|
|
|
发送文件 `/views/index.html` 作为 `/` 的 GET 请求的响应。 如果实时查看应用,你会看到一个大的 HTML 标题(以及我们稍后将使用的表单……),目前它们还没有任何样式。
|
2019-05-06 11:31:26 +00:00
|
|
|
|
2021-02-16 21:01:20 +00:00
|
|
|
**注意:** 你可以编辑上一个挑战的解题代码,或者创建一个新的代码片段。 如果你创建一个新的代码片段,请记住 Express 会从上到下匹配路由,并执行第一个匹配的处理程序, 你必须注释掉前面的代码,否则服务器还是响应之前的字符串。
|
2019-05-06 11:31:26 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2019-05-06 11:31:26 +00:00
|
|
|
|
2021-02-16 21:01:20 +00:00
|
|
|
应用应该响应 views/index.html 文件
|
2019-05-06 11:31:26 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
(getUserInput) =>
|
|
|
|
$.get(getUserInput('url')).then(
|
|
|
|
(data) => {
|
|
|
|
assert.match(
|
|
|
|
data,
|
|
|
|
/<h1>.*<\/h1>/,
|
|
|
|
'Your app does not serve the expected HTML'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(xhr) => {
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
}
|
|
|
|
);
|
2019-05-06 11:31:26 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```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.
|
|
|
|
*/
|
|
|
|
```
|