首页 > 解决方案 > 在 NodeJS 中不使用换行符进行日志记录

问题描述

如何在每次没有换行符的情况下记录此数据流?

websocket.on('message', (msg) => {
    console.log(msg.content);
  }
});

以上将导致:

> streamedContent-1
> streamedContent-2
> streamedContent-3..

我想要实现的是:

> streamedContent-n // this line getting updated on each refresh, no new line

标签: javascriptnode.jsbash

解决方案


您可以在 Node 应用程序中使用 c++stdout方法在运行时“编辑”一行。

process.stdout.clearLine()
process.stdout.write(`\r${msg.content}`);

推荐阅读