0
我想調用Node.js中的函數,獲取結果並將它們發送到模板。 results
總是返回空。將變量發送到nodeJs中的jade模板引擎(同步)
怎麼了?
var replace = req.params.replace || "";
var find = req.params.find;
var fs = require("fs");
var dir = "./public/sounds/";
var results = [];
var readFiles = function (root) {
fs.readdir(root, function (err, files) {
if (err) {
console.log(err);
return;
}
files.forEach(function (file) {
console.log(root.concat(file));
fs.stat(root.concat(file), function (err, stat) {
if (stat && stat.isDirectory()) {
readFiles(root.concat(file + "/"));
}
});
if (file.indexOf(find) > 0) {
var oldPath = root.concat(file);
var newPath = oldPath.replace(find, replace).trim();
console.log("Old Path: ", oldPath, " New Path: ", newPath);
fs.rename(oldPath, newPath);
results.push(newPath);
}
})
});
};
readFiles(dir);
res.jsonp({
message: results
});