node.js, write to file

Include the node file system object.

var fs = require('fs');

Write to file, will overwrite the existing content

fs.writeFile('out.txt', "hello, this line written by fs.writeFile function.\n", function(err){
    if(err){console.log(err);}
});

Append to file, will not overwrite the existing content

fs.appendFile('out.txt',"hey, this line is written by fs.appendFile function.\n", function(err){
	if(err){console.log(err)}
});

Synchronously append to file.

for(var i=0; i<100000; i+=1)
{
	fs.appendFileSync('out.txt',"line "+i+"\n");	
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search