首页 > 解决方案 > 在服务器端使用 NodeJS 将 html 转换为纯文本

问题描述

在服务器端,使用 Nodejs。我收到一条包含 HTML 的短信。我想要一个将 html 转换为纯文本的函数。请不要告诉我添加标签<plaintext><pre>. (nodejs中不存在convert_to_html函数)

socket.on('echo', (text) => {
   plaintext = convert_to_html(text);
   socket.emit('echo', {
      message: plaintext
   });
});

理想结果:

输入:<h1>haha i am big</h1>

明文(我想要明文是什么):&lt;h1 &60;haha i am big &lt;/h1 &60;

输出:<h1>haha i am big</h1>

当前结果:

输入:<h1>haha i am big</h1>

纯文本:<h1>haha i am big</h1>

输出:哈哈我很大

标签: htmlcssnode.jsparsingnodejs-server

解决方案


可以在浏览器端使用insertAdjacementHTML方法,这里举个例子

socket.on("response", function (msg) {
  const messages = document.getElementById("messages");
  messages.insertAdjacentHTML("beforebegin", msg);
  window.scrollTo(0, document.body.scrollHeight);
});

推荐阅读