繁体   English   中英

Node.js-从文本文件读取html内容时出现问题

[英]Nodejs - Issue while reading html contents from text file

我想使用下面的代码从文本文件中读取html内容。在使用fs模块从文件中读取所有html标记时,它们被替换为一些垃圾字符。

有人可以帮助我了解我在做什么错吗?

enter code here文件内容

<h2 class="mb30">Bone Grafting</h2>

响应

&lt;h2 class&#x3D;&quot;mb30&quot;&gt;Bone Grafting&lt;&#x2F;h2&gt;

使用的代码

var fs = require('fs');
fs.readFile(filePath, function (err, contents) {
   //some code here
}

文本文件的编码为utf-8

您缺少包含用于读取文件详细信息的文件格式的信息。

fs = require('fs')
fs.readFile('/etc/hosts', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});

暂无
暂无

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

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