Nel tuo esempio, fd
è il secondo parametro del callback
passato a fs.open
:
- Che cosa è importante è che è il secondo parametro.
- Il suo nome non è importante.
Questo secondo parametro rappresenta un file di descrizione, utile per l'accesso che ha aperto il file all'interno della richiamata.
Ad esempio :
var fs = require("fs");
var fileName = "foo.txt";
fs.exists(fileName, function(exists) {
if (exists) {
// get information about the file
fs.stat(fileName, function(error, stats) {
// open the file (getting a file descriptor to it)
fs.open(fileName, "r", function(error, fd) {
var buffer = new Buffer(stats.size);
// read its contents into buffer
fs.read(fd, buffer, 0, buffer.length, null, function(error, bytesRead, buffer) {
var data = buffer.toString("utf8", 0, buffer.length);
console.log(data);
fs.close(fd);
});
});
});
}
});
lì, avete fd
(il secondo parametro di fs.open
) passato a fs.read
come primo parametro, e così si può leggere quel file.
1: Esempio tratto da: http://www.sitepoint.com/accessing-the-file-system-in-node-js/
fonte
2016-04-21 13:45:08
grazie mille. Questo mi ha davvero aiutato. – karthi