【发布时间】:2021-05-25 10:59:30
【问题描述】:
我是 Node.js 的新手,我正在尝试将变量从 Node.js 传递和访问到已加载的 html 文件/template,我该如何实现?
这里是示例代码:
test.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>
myService.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 模板,以便在读取文件时,值的变量已经在模板中生成。
【问题讨论】:
标签: javascript html node.js fs