node js filestream | node js tutorials

 _filename

_dirname

above are two glabal varialbe

_dirname - it prints current directoru

_filename - it printes current location 

process.cwd()

current working directory

process.chdir("") change directory



 



join path and string

path.sep = console = /

directories.join(path.sep);



how to base name from path

how to get ext from path

path.extname(filename)

path.basename(filename)





how to get absolute path

path.resolve(_dirname,'content','subfolder','test.txt')
users/smilga/desktop/tutorial/content/subfolder/test.txt

how to check file exists and 
red file write file then rename the file

fs is one module
var fs= require('fs');
var path= _dirname +"/mytext.txt";

fs.readFile(path, "utf8" function(error,data)

read the file in that path and then encrypt with utf8 then callback the function
i



how to rename the fuile

fs.rename



make new directory



read all the data in directory
fs.readdir(path, function (error,files){




remove directory
fs.rmdir




const {readFileSync,writeFileSync} = require('fs');

read the file 
const first = readfilesync('pathname','utf8');
write into file
write from another files too

writefilesync('path','hellow world : ${first}, ${second}',{flag:'a'})

flag new line

 

flag a means append
 


create server , console the data from file

it wil print the whole data its in mbs if it is a larage file


fs.craetereadstream


same size in ui but in response headers , transfer-encoding - chunked






readfile('path',(){})
call back function with arguments
readfile('path',(err,result){
    if(err)
{
    console.log(err)
return
}
console.log(result)
})

without utf8



readfile('path','utf8',(err,result){
    if(err)
{
    console.log(err)
return
}
console.log(result)
const first : result
writeFIle('path','here $(furst)',(err,resulte)=>{})
})

write file



read and write into same file



whats the difference between read file and read file sync

if async- if a function takes time - taht function execute late, whichone execute first that one wil execute
before excute of readfiile sync console works
but line by line works which one take large time that throw to last time means it runs on sepaerately
if it works over it prints immedieatly

next line not stop for before line
sync




async
readFile



with promise
now below works as sync
means when this called it must return becaue of promise  (call back)
till that next line waits






const start = async() => {}
call back in async funciton
const start = async() => {
const first = await getText('./content/first.txt');
console.log(first);

}
in try catch
one function for number of file paths
const start = async() => {
try{
const first = await getText('./content/first.txt');
const second = await getText('./content/second.txt');
console.log(first);
}catch(error)[
console.log(error)
}

}
 


calling above function - invoke
start();

instead of writing get text function seperate use
util

util is a ibuilt funciton




without util and with promises

 




in above thing, if read one file that file data write to another fileif the next funciton in next line , its not wokrs because it is async
so for that purpose in call back we write another funciion like below












No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...