简体   繁体   English

如何使用 fs.writefile 创建 HTML 文件

[英]How to create an HTML file using fs.writefile

I need to create an HTML file using fs.writefile .我需要使用fs.writefile创建一个 HTML 文件。 I now have a txt file ( menuOutput.txt ) with all the information that I need to display on the HTML.我现在有一个 txt 文件 ( menuOutput.txt ),其中包含我需要在 HTML 上显示的所有信息。 It looks like this:它看起来像这样:

Lunch Items午餐项目

17.26 bento box b - sashimi, box combo 17.26 便当盒 b - 生鱼片,盒装

15.46 bento box a - chicken teriyaki, box combo 15.46 便当盒 a - 照烧鸡肉,盒装

Dinner Items晚餐项目

6.30 vegetable sushi, 6 rolls 6.30 蔬菜寿司,6 卷

8.10 tuna roll, 3 rolls 8.10 金枪鱼卷,3 卷

7.11 roe, 2 rolls 7.11 鱼子,2 卷

Function createHTML (textFile) {
    return new Promise ((resolve, reject)=>{
        fs.readFile(textFile, 'utf8', (err,data) => {
            if (err) {
                reject(err)
            }
            resolve (data)
        })
    })
}

createHTML('menuOutput.txt') 
        .then(data=>{
            fs.writeFile ('index.html', data, (err) => {
                console.log (data)
                if (err) {
                    if (err) throw err
                    console.log ('created')

                }
            })
        })

However, my HTML file is just a single line of text, how can I implement HTML tag in the function?但是,我的 HTML 文件只是一行文本,如何在 function 中实现 HTML 标记? Or is there anything wrong with my way of thinking?还是我的思维方式有什么问题?

You can try this code modification.你可以试试这个代码修改。

createHTML('menuOutput.txt').then(data=>
    {
        var stream = fs.createWriteStream('output.html', {flags: 'a'});
        stream.write(data, function() {
        var html = buildHtml();
        stream.end(html);
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM