Node 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 是你的朋友,学习 through 或 through2 对你在 node (streams) 中有帮助。
以下,code/pseudo
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);
});
在 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 是你的朋友,学习 through 或 through2 对你在 node (streams) 中有帮助。
以下,code/pseudo
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);
});