代码区项目交易流程

nodejs prepending to a file


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.

Problem courtesy of: bluejamesbond

Solution

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.

Solution courtesy of: Benjamin Gruenbaum

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

点击收藏

LAST Asynchronous response Ajax does not load in div Reviewing is Everything: A tool that makes assessing candidates easier NEXT