You can use the f.readdir[]
method to list all files available in a directory in Node.js. This method asynchronously reads the contents of the given directory and returns an array of the file names excluding .
and ..
.
Here is an example that reads all files available in a directory and prints their names on the console:
const fs = require['fs']
// directory path
const dir = './node_modules/'
// list all files in the directory
fs.readdir[dir, [err, files] => {
if [err] {
throw err
}
// files object contains all files names
// log them on console
files.forEach[file => {
console.log[file]
}]
}]
The fs.readdir[]
method will throw an error if the given directory doesn't exist.
Alternatively, you can use the readdirSync[]
method that works synchronously and returns an array of the names of the files:
To serve static files such as images, CSS files, and JavaScript files, use the express.static
built-in middleware function in Express.
The function signature is:
express.static[root, [options]]
The root
argument specifies the root directory from which to serve static assets. For more information on the options
argument, see .
For example, use the following code to serve images, CSS files, and JavaScript files in a directory named
app.use[express.static['public']]
0:app.use[express.static['public']]
Now, you can load the files that are in the
app.use[express.static['public']]
0 directory://localhost:3000/images/kitten.jpg
//localhost:3000/css/style.css
//localhost:3000/js/app.js
//localhost:3000/images/bg.png
//localhost:3000/hello.html
Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL.
To use multiple static assets directories, call the express.static
middleware function multiple times:
app.use[express.static['public']]
app.use[express.static['files']]
Express looks up the files in the order in which you set the static directories with the express.static
middleware function.
NOTE: For best results, cache to improve performance of serving static assets.
To create a virtual path prefix [where the path does not actually exist in the file system] for files that are served by the express.static
function, for the static directory, as shown below:
app.use['/static', express.static['public']]
Now, you can load the files that are in the
app.use[express.static['public']]
0 directory from the app.use[express.static['public']]
6 path prefix.//localhost:3000/static/images/kitten.jpg
//localhost:3000/static/css/style.css
//localhost:3000/static/js/app.js
//localhost:3000/static/images/bg.png
//localhost:3000/static/hello.html
However, the path that you provide to the express.static
function is relative to the directory from where you launch your
app.use[express.static['public']]
8 process. If you run the express app from another directory, it’s safer to use the absolute path of the directory that you want to serve: The file path represents the path of the directory executing the file. The callback function returns an error or an array of files. You can catch the error. Or loop through the files array and print them.
The
mkdir child_dir
touch file.py file1.js file2.c
4 module implements both asynchronous and synchronous file handling options. As a result, you can also Node.js get all files in directory is straightforward after understanding synchronous and asynchronous directory reading using the mkdir child_dir
touch file.py file1.js file2.c
4, mkdir child_dir
touch file.py file1.js file2.c
8, and the const fs = require['fs']
const path = require['path']
const fullPath = path.join[__dirname, 'target_dir']
const files = fs.readdirSync[fullPath]
try { files.forEach[ file => console.log[file] ] }
catch [error] { console.log[error] }
7 modules, as explained in this tutorial. Besides, you can recursively get all files using the mkdir target_dir
cd target_dir
07 module.