How does fs.stat () work?
If you want to use the callback / async fs function, do not use the synchronous version, use fs.stat ():
var fs = require('fs'); console.log("+++++++++++++++++++++++++++++++++++++++"); fs.stat(pathname, function(err, stats) { console.log(stats.isDirectory()); }); console.log("+++++++++++++++++++++++++++++++++++++++");
More information on fs.stat () . You can get a lot of information about the main object:
fs.stat(path, function(err, stats) { console.log(stats) }
Exit:
{ dev: 2049, ino: 305352, mode: 16877, nlink: 12, uid: 1000, gid: 1000, rdev: 0, size: 4096, blksize: 4096, blocks: 8, atime: '2009-06-29T11:11:55Z', mtime: '2009-06-29T11:11:40Z', ctime: '2009-06-29T11:11:40Z' }
Many elements are often useless to us, yes. But here is the meaning of all these variables, in accordance with this article :
- dev: identifier of the device containing the file
- mode: file protection
- nlink: number of hard links to a file
- uid: user ID of the owner of the file.
- gid: identifier of the group of file owners.
- rdev: device identifier if the file is a special file.
- blksize: block size for file system I / O.
- ino: The inode file number. An indent is a file system data structure that is -
- stores file information.
- size: total file size in bytes.
- blocks: the number of blocks allocated for the file.
- atime: a date object representing the time the file was last accessed.
- mtime: a date object representing the time the file was last modified.
- ctime: a date object representing the last time the inode file was modified.
You can also, for example, the JS node documentation , get additional information, for example:
stats.isFile() stats.isDirectory() stats.isBlockDevice() stats.isSymbolicLink() (only valid with fs.lstat()) stats.isCharacterDevice() stats.isFIFO() stats.isSocket()
About stats.isSymbolicLink (), there is another function than fs.stat called fs.lstat (), and here is the difference between them:
stat follows symbolic links. When a path that is a symbolic link is given, it returns the target stat of the symbolic link.lstat does not match symbolic links. When a path that is a symbolic link is given, it returns the stat of the symbolic link, not its purpose.