freeCodeCamp/curriculum/challenges/chinese/05-apis-and-microservices/basic-node-and-express/serve-static-assets.md

2.0 KiB

id challengeType forumTopicId title
587d7fb0367417b2b2512bf0 2 301518 服务静态资源

Description

HTML 服务器通常有一个或多个用户可以访问的目录。你可以将应用程序所需的静态资源 (样式表、脚本、图片) 放在那里。在 Express 中你可以使用中间件express.static(path)来设置此功能,它的参数就是静态资源文件的绝对路径。如果你不知道什么是中间件,也不用担心。我们稍后将详细讨论此事。一个最基本的中间件可以看做是一个函数,它拦截路由处理方法,并在里面添加了一点别的信息。使用app.use(path, middlewareFunction)方法来加载一个中间件。它的第一个参数是可选的,如果没设置第一个参数,那么应用的所有请求都会经过这个中间件处理。

Instructions

使用app.use()来加载express.static()中间件,让所有的请求都能访问我们的静态资源目录。静态资源的绝对路径是__dirname + /public。 现在 app 就能正常返回一个样式文件了,访问 app 根路径时,静态资源目录的文件就会被加载进来,现在你的首页看起来应该好些了!

Tests

tests:
  - text: 应用的静态资源文件应该来自<code>/public</code>目录
    testString: 'getUserInput => $.get(getUserInput(''url'') + ''/style.css'').then(data => { assert.match(data, /body\s*\{[^\}]*\}/, ''Your app does not serve static assets''); }, xhr => { throw new Error(xhr.responseText); })'

Challenge Seed

Solution

/**
  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.
*/