如何在html中使用express中的变量?

问题描述

我有这个快递

app.get('/',requiresAuth(),(req,res) =>
    res.sendFile('/db_auth_site/index.html',{ title: 'hey',message: 'Hello there!' })
);

如何在 html 中使用标题

<h1>title variable here</h1>

解决方法

你本身不能。 HTML 没有这种能力。

您需要使用模板语言,例如 NunjucksEJSMustache

Express documentation has a section that covers using PugMDN has a more detailed guide

其他模板语言类似,您只需更改您安装的模块和 view engine 设置,然后使用您选择的模板语言的语法。

请注意,大多数模板语言都是“您要输出的带有特殊标签的语言”,但 Pug 是一个例外。如果您想编写看起来像 HTML 的内容,请不要使用 Pug。

您还需要使用 render 而不是 sendFile