![](/img/trans.png)
[英]How To Read and Print Data from CSV file into HTML File as Text
[英]How to read data from text file, change the data and append it to a html?
我的文本文件中包含數據,其中包含帶有這樣的模板文字的HTML
<html>
<head></head>
<body>
<p>`${abc}`</p>
</body>
</html>
我有來自服務器abc的數據。我希望節點從.txt文件中讀取此數據,用數據替換模板文字,然后將其附加到另一個.html文件中。
var abc = "Hi there"
var dat = fs.readFileSync('home.txt', 'utf8');
fs.writeFile('index.html', dat, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
我要去哪里錯了?
最好使用諸如Mustache之類的模板引擎。 例如:
您的txt文件:
<html>
<head></head>
<body>
<p>{{abc}}</p>
</body>
</html>
和你的代碼:
const Mustache = require('mustache')
var abc = "Hi there"
var dat = fs.readFileSync('home.txt', 'utf8');
var html = Mustache.render(dat, {abc: abc});
fs.writeFile('index.html', html, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.