Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close file after fs.readFile, nulling

Tags:

node.js

fs

Just try nodejs a few days. Should i close somehow files after fs.readFile() ? And if yes how? Should i null or something used and needless variables for better perfomance and memory safe?

Thanks.

like image 528
NoWayOut Avatar asked Feb 03 '14 09:02

NoWayOut


People also ask

Which method of fs is used to close a file?

close() Method. The fs. close() method is used to asynchronously close the given file descriptor thereby clearing the file that is associated with it. This will allow the file descriptor to be reused for other files.

Does readFile return anything?

readFile. Returns the contents of the file named filename. If encoding is specified then this function returns a string. Otherwise it returns a buffer.

How do I get data from fs readFile?

var content; fs. readFile('./Index. html', function read(err, data) { if (err) { throw err; } content = data; }); console.

What Is syntax of readFile () method?

Syntax: fs.readFile( filename, encoding, callback_function ) Parameters: The method accept three parameters as mentioned above and described below: filename: It holds the name of the file to read or the entire path if stored at other location. encoding: It holds the encoding of file. Its default value is 'utf8'.


1 Answers

to close a file descriptor use this call

fs.close(fd, callback)

however since you're using fs.readFile which reads the entire file - it deals with opening and closing file for you, so you don't need to close anything

like image 89
dark_ruby Avatar answered Oct 10 '22 02:10

dark_ruby