nodejs前置于文件 [英] nodejs prepending to a file

查看:83
本文介绍了nodejs前置于文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Node.js,以类似方式预装到文件的最佳方式是什么

For Node.js, what is the best way to prepend to a file in a way SIMILAR to

fs.appendFile(path.join(__dirname, 'app.log'), 'appendme', 'utf8')

就个人而言,最好的方法是围绕创建日志的异步解决方案,我可以从顶部推送到该文件。

Personally, the best way really revolves around a asynchronous solution to create a log where I can basically push onto the file from the top.

推荐答案

无法添加到文件的开头。 请参阅此问题,了解C中的类似问题或这个问题

It is impossible to add to a beginning of a file. See this question for the similar problem in C or this question for the similar problem in C#.

我建议你以常规方式进行日志记录(即登录到文件末尾)。

I suggest you do your logging in the conventional way (that is, log to the end of file).

否则,无法绕过读取文件,将文本添加到开头并将其写回文件,这可以非常快速地获得成本。

Otherwise, there is no way around reading the file, adding the text to the start and writing it back to the file which can get really costly really fast.

这篇关于nodejs前置于文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆