To write on a file in Node.js this is the usual process:
var fs = require('fs'); //[...] fs.writeFile('log.txt', 'Hello', encoding='utf8', function (err) { if (err) throw err; });
But this would overwrite the content of the file and write 'Hello' in it. To append text on a file, recently Node added the following command to their APIs:
var fs = require('fs'); //[...] fs.appendFile('log.txt', 'Hello', encoding='utf8', function (err) { if (err) throw err; });
As you can see it works exactly like the writeFile function except that this would append 'Hello' at the end of the file. Moreover, if the file doesn't exist yet, it will create it for you. Pretty useful!
What I noticed while looking at the file that was filled in the meantime with IDs (I kept inspecting it through wc -l filename) is that sometimes it "blocked" itself on some values, and then after a while it added a bunch of IDs and then again it blocked, and so on. I was pretty frustrated by this, since I thought it was a problem of my topology. In the end I asked Google about the appendFile function, since I was interested in its performances and I found out that it is asynchronous. That means I never had any error concerning the odd behaviour it had, it was just because the execution of the function itself is asynchronous. Thus I searched for a synchronous way to append text on a file and on the Node API page I found this:
var fs = require('fs'); //[...] fs.appendFileSync('log.txt', 'Hello', encoding='utf8');
As you can see it works a little bit differently from what appendFile does: it doesn't take a callback function anymore. And that's it. Now it appends correctly each time the process receives a message, so I can better monitor what's going on.
Hello,
ReplyDeleteI had the same problem (needing to persist data without having to install a full-blown database) so I wrote a pure javascript database that can persist to a file (using the appendFile function). It provides the most common subset of MongoDB's API, for clean CRUD operations: https://github.com/louischatriot/nedb
Thanks for the pointer, looks awesome!
ReplyDelete@Masiar Thanx
ReplyDeleteThank you for your valuable information about the node.js development security this is an useful one keep sharing the post like this...
ReplyDeletenode.js development services
kuşadası transfer
ReplyDeletefoça transfer
alaçatı transfer
didim transfer
karşıyaka transfer
QU8H0C
yozgat
ReplyDeletesivas
bayburt
van
uşak
FZ0
görüntülüshow
ReplyDeleteücretli show
SXKQ
Adıyaman Lojistik
ReplyDeleteTrabzon Lojistik
Muğla Lojistik
Bayburt Lojistik
Bayburt Lojistik
4P5
2FC4E
ReplyDeleteAntalya Evden Eve Nakliyat
Burdur Evden Eve Nakliyat
Siirt Evden Eve Nakliyat
Çanakkale Evden Eve Nakliyat
Trabzon Evden Eve Nakliyat
6FD39
ReplyDeleteKastamonu Evden Eve Nakliyat
Mardin Lojistik
Denizli Evden Eve Nakliyat
Bitlis Evden Eve Nakliyat
Karabük Parça Eşya Taşıma
8DD87
ReplyDeleteBitrue Güvenilir mi
Kocaeli Parça Eşya Taşıma
Kırıkkale Lojistik
Kütahya Parça Eşya Taşıma
Pursaklar Fayans Ustası
Isparta Lojistik
Muğla Lojistik
Çankaya Fayans Ustası
Urfa Evden Eve Nakliyat
F72C3
ReplyDeleteYalova Parça Eşya Taşıma
Artvin Evden Eve Nakliyat
Eskişehir Lojistik
Çanakkale Şehirler Arası Nakliyat
Urfa Parça Eşya Taşıma
Aksaray Şehir İçi Nakliyat
Edirne Şehirler Arası Nakliyat
Tokat Lojistik
Gümüşhane Şehirler Arası Nakliyat
94B8F
ReplyDeleteCate Coin Hangi Borsada
Ankara Fayans Ustası
Baby Doge Coin Hangi Borsada
Muğla Lojistik
Hatay Şehirler Arası Nakliyat
Siirt Evden Eve Nakliyat
Ağrı Şehirler Arası Nakliyat
Diyarbakır Lojistik
Uşak Evden Eve Nakliyat
D41EA
ReplyDeleteSilivri Fayans Ustası
Ünye Mutfak Dolabı
Konya Evden Eve Nakliyat
Keçiören Boya Ustası
Burdur Parça Eşya Taşıma
Sakarya Evden Eve Nakliyat
Rize Lojistik
Keep Coin Hangi Borsada
Zonguldak Lojistik
8BC65
ReplyDelete%20 indirim kodu
547D0
ReplyDeletereferanskodunedir.com.tr
461F9
ReplyDeleteEskişehir Yabancı Görüntülü Sohbet
amasya görüntülü sohbet sitesi
sakarya canlı sohbet ücretsiz
erzurum goruntulu sohbet
sohbet odaları
kadınlarla sohbet
Adıyaman Canli Goruntulu Sohbet Siteleri
antalya kızlarla rastgele sohbet
Amasya Görüntülü Sohbet Uygulama
95DF0
ReplyDeleteücretsiz sohbet
artvin mobil sohbet odaları
bitlis parasız sohbet
erzurum ücretsiz sohbet uygulaması
Çanakkale Canlı Görüntülü Sohbet Odaları
mardin mobil sohbet chat
Kırıkkale Bedava Sohbet Uygulamaları
niğde telefonda kızlarla sohbet
artvin rastgele görüntülü sohbet ücretsiz
182D1
ReplyDeleteKripto Para Üretme
Kripto Para Nasıl Kazılır
Referans Kimliği Nedir
Kwai Takipçi Satın Al
Nexa Coin Hangi Borsada
Bitcoin Çıkarma Siteleri
Mexc Borsası Kimin
Spotify Dinlenme Satın Al
Tumblr Takipçi Satın Al