I'm pretty new to Node so I apologize if this is a simple question. But I'm trying to read the contents of a directory, ./resources
, then display links to them on the webpage. The catch is that the files in the directory are dynamic, so I'm using fs.readdir
in Node.
But the <h1>
is not showing on the index.html
page; any ideas why?
const resDir = "resources/";
const resDirFiles = [];
const app = http.createServer((req, res) => {
...
fs.readFile(filePath, (err, content) => {
if (err) {
// To be implemented
} else {
res.writeHead(200, {
"Content-type": contentType
});
res.end(content, "utf8", callback(req, res));
}
});
});
function callback(req, res) {
if (req.url == "/" || req.url == "/index") {
fs.readdir(resDir, (err, files) => {
files.forEach(file => {
resDirFiles.push(file);
res.end("<h1>Ok</h1>"); // placeholder
});
});
}
}