node loop files and push to array to display on screen with nunjucks

Solutions on MaxInterview for node loop files and push to array to display on screen with nunjucks by the best coders in the world

showing results for - "node loop files and push to array to display on screen with nunjucks"
Israel
12 Mar 2020
1<!-- Nunjucks to loop the array and show each file in a list -->
2<ul>
3  {% for file in fileList %}
4  	<li>{{ file }}</li>
5  {% endfor %}
6</ul>
Isidora
27 Mar 2018
1const directoryPath = './path/to/dir/';
2
3// loop files in directory and push to an array and pass to rendered screen
4router.get('/slug', (req, res) => {
5    let fileList = [];
6    fs.readdir(directoryPath, (err, file) => {
7        if (err) {
8            return console.log('Unable to scan directory: ' + err);
9        } 
10        file.forEach((file) => {
11            fileList.push(file);
12        });
13      	// Passing the array to the rendered screen
14        res.render('rendered/screen', {fileList});
15    });
16});
similar questions