簡體   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