繁体   English   中英

节点Winston日志文件强制字符串转换

[英]Node Winston log file forced string conversion

在Node项目中,我想在React界面中显示Winston日志文件的内容。 读取文件:

let content;
fs.readFile("path", "utf-8", function read(err, data) {
  if (err)
      throw err;

 content = data; 
 });

我将它们发送到界面:

router.get("/", function (req, res) {
    res.status(200).send(JSON.stringify(content));
});

我将内容保存在.jsx文件中:

getLogs().then(res => {
            let datafromfile = JSON.parse(res);
            // Use the data
            return;
        }).catch(err => {
            return err.response;
        });

我遇到的问题是fs将所有数据都转换为字符串(因为我正在输入utf-8编码并且不想返回缓冲区),因此我无法操纵日志文件中的对象以结构方式显示它们在界面中。 谁能指导如何解决这个问题?

我还没有调试过,但是很多取决于您所加载的Winston文件中是否确实包含JSON。

如果确实如此,则JSONStream是您的朋友,并且通过或直通2对节点(流)很有帮助。

以下,代码/伪

router.get("/", function (req, res) {
    const logPath = ‘somePath’; // maybe it comes from the req.query.path
    const parsePath = null; // or the token of where you want to attemp to start parsing
    fs.createReadStream(logPath)
      .pipe(JSONStream.parse(parsePath))
      .pipe(res);
});

暂无
暂无

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

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