Home > Article > Web Front-end > How to read file directory in nodejs
Node.js is an open source, cross-platform JavaScript runtime environment that can be used not only for server-side development, but also for client-side development. In Node.js, we can use the fs module to read file directories.
fs module is a module used to interact with the file system. It provides some common file operation functions, including reading and writing files, creating and deleting directories, etc. Using the fs module to read file directories can use the fs.readdir(), fs.readdirSync() and fs.stat() methods.
The fs.readdir() method can read the names of all files and subdirectories under the specified path and return these files in the form of an array and the name of the subdirectory. The first parameter of this method is the directory path to be read, and the second parameter is a callback function. The err parameter in this function represents the error message when reading the directory, and the files parameter is the list of files read.
The following is a sample code that uses fs.readdir() to read a file directory:
const fs = require('fs'); const path = './filedir';//目录路径 fs.readdir(path, function(err, files) { if (err) { console.error('读取目录出错:', err); return; } console.log('读取到的文件列表:', files); });
Run this sample code, the console will output the file and subdirectory names in the file directory. It should be noted that the fs.readdir() method is asynchronous and requires a callback function to obtain the result.
The fs.readdirSync() method is similar to the fs.readdir() method, but it is synchronous and will block the thread until the file directory Read completed. The first parameter of this method is the directory path to be read, and the return value is the list of files read.
The following is a sample code that uses fs.readdirSync() to read a file directory:
const fs = require('fs'); const path = './filedir';//目录路径 try { const files = fs.readdirSync(path); console.log('读取到的文件列表:', files); } catch (err) { console.error('读取目录出错:', err); }
Run this sample code, the console will also output the file and subdirectory names in the file directory. It should be noted that using the fs.readdirSync() method will block the Node.js event loop.
fs.stat() method is used to obtain the status information of a file or directory, including file size, modification time, access time, etc. . The first parameter of this method is the file path to obtain the status information. The second parameter is a callback function. The err parameter in this function represents the error message when reading the file status information. The stats parameter is the read file. status object.
The following is a sample code that uses fs.stat() to read the file status:
const fs = require('fs'); const path = './filedir';//文件路径 fs.stat(path, function(err, stats) { if (err) { console.error('获取文件状态出错:', err); return; } console.log('文件大小:', stats.size); console.log('修改时间:', stats.mtime); console.log('访问时间:', stats.atime); });
Run this sample code, the console will output the file size, modification time, access time and other information. It should be noted that the fs.stat() method is also asynchronous and requires a callback function to obtain the results.
In summary, using the fs module of Node.js to read file directories requires the use of fs.readdir(), fs.readdirSync() and fs.stat() methods. Choose the corresponding method according to actual needs. . When using these methods, possible error messages need to be handled to ensure the stability and reliability of the program.
The above is the detailed content of How to read file directory in nodejs. For more information, please follow other related articles on the PHP Chinese website!