freeCodeCamp/curriculum/challenges/chinese/05-apis-and-microservices/basic-node-and-express/serve-an-html-file.chinese.md

1.7 KiB
Raw Blame History

id title localeTitle challengeType
587d7fb0367417b2b2512bef Serve an HTML File 提供HTML文件 2

Description

我们可以使用res.sendFile(path)方法响应文件。 您可以将它放在app.get('/', ...)路由处理程序中。在幕后此方法将根据其类型设置适当的标头以指示您的浏览器如何处理您要发送的文件。然后它将读取并发送文件。此方法需要绝对文件路径。我们建议您使用Node全局变量__dirname来计算路径。 例如:
absolutePath = __dirname + relativePath/file.ext

Instructions

要发送的文件是/views/index.html 。尝试“显示”你的应用程序你应该看到一个很大的HTML标题以及我们稍后将使用的表单......),没有应用任何样式。

注意: 您可以编辑上一个挑战的解决方案也可以创建一个新挑战。如果您创建新解决方案请记住Express会从上到下评估路由。它执行第一个匹配的处理程序。您必须注释掉前面的解决方案否则服务器将继续使用字符串进行响应。

Tests

tests:
  - text: 您的应用应该提供文件views / index.html
    testString: '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); })'

Challenge Seed

Solution

// solution required