I've built this endpoint:
app.get("/files", async (req, res) => {
const getFilesFromLoc = async loc => {
let filesString = "";
await fs.readdir(loc, async (err, files) => {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
return await files.forEach(async file => {
const newPath = path.join(loc, file);
return await fs.stat(newPath, async (err, stat) => {
if (err) {
console.error("Error stating file.", err);
return;
}
if (stat.isFile()) {
const extension = path.extname(newPath);
if (extension === ".js" || extension === ".jsx") {
return await fs.readFile(newPath, "utf8", (err, string) => {
if (err) {
console.error("Error reading file.", err);
return;
}
return filesString += string;
});
}
} else if (stat.isDirectory()) {
return await getFilesFromLoc(newPath);
}
})
})
})
return filesString;
}
const filesString = await getFilesFromLoc("src/client/actions");
res.send(filesString);
});
What it does is from a starting path, it collects every js and jsx file, converts it into a string, and pushes it to the others in fileString
.
The function works well, however. the response is an empty string. Where am I doing wrong?