freeCodeCamp/curriculum/challenges/chinese/06-information-security-and.../advanced-node-and-express/set-up-a-template-engine.ch...

2.7 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
5895f700f9fc0f352b528e63 Set up a Template Engine 2 设置模板引擎

Description

提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。模板引擎使您可以在应用程序中使用静态模板文件(例如用Pug编写的文件。在运行时模板引擎将模板文件中的变量替换为可由服务器提供的实际值并将模板转换为静态HTML文件然后将其发送到客户端。这种方法可以更轻松地设计HTML页面并允许在页面上显示变量而无需从客户端进行API调用。要设置Pug以便在项目中使用您需要先在package.json中将其作为依赖项添加。 "pug": "^0.1.0"现在告诉Node / Express使用模板引擎你必须告诉你的快递应用程序 'pug' 设置为'view-engine'。 app.set('view engine', 'pug')最后,你应该改变请求您响应该指数路线res.render与路径视图意见/哈巴狗/ index.pug。如果一切按计划进行您应该刷新应用程序主页并看到一条小消息说您已成功从我们的Pug文件中删除Pug当您认为自己已经做对时请提交您的页面。

Instructions

Tests

tests:
  - text: 帕格是一个依赖
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/package.json") .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, "pug", "Your project should list "pug" as a dependency"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: 查看引擎是帕格
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /("|")view engine("|"),( |)("|")pug("|")/gi, "Your project should set Pug as a view engine"); }, xhr => { throw new Error(xhr.statusText); })'
  - text: 帕格正在工作
    testString: 'getUserInput => $.get(getUserInput("url")+ "/") .then(data => { assert.match(data, /pug-success-message/gi, "Your projects home page should now be rendered by pug with the projects .pug file unaltered"); }, xhr => { throw new Error(xhr.statusText); })'

Challenge Seed

Solution

// solution required