K
K
komarevtsev2016-04-22 14:49:23
Node.js
komarevtsev, 2016-04-22 14:49:23

Write to end of fs file?

I want to write to the end of the file. I do this:
https://jsfiddle.net/h7td34wc/
but it seems to me that opening a file for writing every time is not ok. Is there any way to record without opening?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynn "Coffee Man", 2016-04-22
@komarevtsev

> whether it is possible to write down somehow without opening?
No, but there are methods that will do it for you. For example fs.appendFile(fs.appendFileSync)

S
Super User, 2016-04-22
@sergeystepanov1988

but it seems to me that each time opening a file for writing is not ok

That's right, if there is a lot of data to write and it comes often, then it is better to open the file once and write it in a stream:
var fs = require("fs");

var writeStream = fs.createWriteStream("log.txt"); // создаем поток
writeStream.write("Hello world!"); // пишем
writeStream.end(); // закрываем

In general, for logging it is better to use libraries sharpened for this, for example Winston .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question