Is there a way to transfer files from the server for display in the browser within the index pug file?
extends layout block content h1= 'saved files' #recordings.recordings.row script(src='/javascripts/listrecordings.js')
function fetchRecordings() {
fetch('/')
.then((response) => response.json())
.then((response) => {
console.log("response " + response);
if (response.success && response.files) {
//remove all previous recordings shown
recordingsContainer.innerHTML = '';
response.files.forEach((file) => {
//create the recording element
const recordingElement = createRecordingElement(file);
//add it the the recordings container
recordingsContainer.appendChild(recordingElement);
})
}
})
.catch((err) => console.error(err));
};
Here is the relevant part of the index js:
router.get('/', (req, res) => {
let files = fs.readdirSync('./public/upploads');
console.log(__dirname + " files length " + files.length);
files = files.filter((file) => {
// check that the files are audio files
const fileNameArr = file.split('.');
return fileNameArr[fileNameArr.length - 1] === 'mp3';
}).map((file) => `/${file}`);
//res.json({ success: true, files });
res.render('index', files);// stuck here ???
});
Can anyone help with sending a response to the index in this scenario?