node loop files and push to array to display on screen with nunjucks
<!-- Nunjucks to loop the array and show each file in a list -->
<ul>
{ % for file in fileList %}
<li>{{ file }}</li>
{ % endfor %}
</ul>const directoryPath = './path/to/dir/';
// loop files in directory and push to an array and pass to rendered screen
router.get('/slug', (req, res) => {
let fileList = [];
fs.readdir(directoryPath, (err, file) => {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
file.forEach((file) => {
fileList.push(file);
});
// Passing the array to the rendered screen
res.render('rendered/screen', {fileList});
});
});
Also in JavaScript:
- useeffect react
- how to use a js class in react
- equal to or more than javascript
- jquery get current row value
- javascript angle equation of a line
- Failed to execute 'contains' on 'Node': parameter 1 is not of type 'Node'
- get attribute value jquery href
- reduce method javascript
- jquery test div exists
- loop through api response in react
- break in if statement js
- jquery OR operator
- mongoose database pooling
- call method in javascript
- node js sendgrid
- charat javascript
- open folder node js
- javascript queryselector
- FizzBuzz
- group all items with same name js
- json unmarshal golang example
- instanceof javascript
- dockerfile for nodejs
- jquery detect nav tab change event