簡體   English   中英

如何從文本文件讀取數據,更改數據並將其附加到html?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM