javascript - 如何将 node.js 变量传递/访问到 html 文件/模板
问题描述
我是Node.js的新手,我正在尝试将一个变量从Node.js传递和访问到加载的html文件/模板,我该如何实现?
这是示例代码:
测试.html
<!DOCTYPE html>
<html>
<head>
<mate charest="utf-8" />
<title>Hello world!</title>
</head>
<body>
<h1>User List</h1>
<ul>
<li>Name: Name</li> <!-- how do I call the name variable here -->
<li>Age: Age</li> <!-- how do I call the age variable here -->
<br>
</ul>
</body>
</html>
我的服务.js
let fs = require('fs');
let path = require('path');
// How do I pass this variables to test.html
let age = 1;
let name = "this is name";
// Read HTML Template
let html = fs.readFileSync(path.resolve(__dirname, "../core/pdf/test.html"), 'utf8');
如何将name和age变量传递和访问到test.html模板,以便在读取文件时,变量的值已经在模板中生成。
解决方案
为此,您需要使用 express 和模板引擎。请参考模板引擎列表 - https://expressjs.com/en/resources/template-engines.html
推荐阅读
- python - 是否有一种简单的方法可以通过定义的 python 函数与 GPU 进行并行处理?
- css - 为下拉菜单的最后一个元素设置样式 React Native
- python - 在 docker-compose 中运行 django 和 cron
- python - 如何在 Django 中使用两个 ForeignKey 字段在 ModelForm 中保存模型
- google-cloud-platform - Apache Beam 转换中对数据存储的同步调用的高挂墙时间
- julia - 在 Julia 中降级到微分方程 v6.0
- node.js - 为什么 Eslint 插件在本地找到,但我无法在服务器上构建?
- python-3.x - 如果 @timestamp 是上一个日期,则 Cloudwatch 查询不起作用
- reactjs - “或”运算符不适用于 window.location.pathname
- python - Numpy Pivot 到数据框但保留索引