Using require, and importing standard nodejs module Filesystem and os available at(nodejs.org/api)
//this is the file we will be running from terminal
console.log('Starting Project')
//load standard node module from nodejs.org/api
const fs = require('fs');
const os = require('os');
fs.appendFile('Welcome.txt','Hello '+os.userInfo().username+' !',(error)=>{
if(error){
console.log('Unable to write')
}
})
fs.appendFile
-> first parameter: filename, it will create if it does not exist
-> 2nd parameter: text, which will be written to file
-> 3rd parameter: callback function: while processing in case of any error, error will be thrown
No comments:
Post a Comment