我对 Javascript/NodeJS(第一次)相当陌生,并且一直遇到内存泄漏错误的问题。我不断收到以下信息:
检测到可能的 EventEmitter 内存泄漏。添加了 11 个 SIGINT 侦听器。使用emitter.setMaxListeners() 增加限制
我似乎找不到修复它的方法。我尝试使用require('events').EventEmitter.defaultMaxListeners = 0;
似乎在 localhost 上工作,但在 linux 服务器上运行它会给我多个“子进程 xxxx 仍然没有退出,发送 SIGTERM”错误,并迫使我不得不重新启动服务器。
var html = require('pa11y-reporter-html');
var pa11y = require('pa11y');
var fs = require("fs");
//require('events').EventEmitter.defaultMaxListeners = 0;
async function runPa11y(url) {
try {
let results = await pa11y(url);
let htmlResults = html.results(results);
return htmlResults
} catch (err) {
console.log("Error: " + err)
}
}
function listScript() {
const args = process.argv;
const os = require('os');
const siteName = args[2];
pathToSiteDir = os.homedir() + "/" + siteName
try {
fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'w');
} catch (err) {
console.log("Could not open results.html" + err)
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
var array = fs.readFileSync(pathToSiteDir + "/crawled.txt").toString().split("\n");
array = array.filter(function(entry) { return entry.trim() != ''; });
(function theLoop (i) {
setTimeout(function () {
console.log("url: " + array[i])
let reply = runPa11y(array[i])
process.removeAllListeners('exit')
reply.then(function(result) {
try {
fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'a');
fs.appendFileSync(fd, result + "<br>", 'utf8');
} catch (err) {
console.log("Could not open results.html" + err)
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
});
--i
if (i >= 0) {
theLoop(i);
console.log("Links left to audit: " + i)
}
}, 300);
})(array.length -1);
}
listScript()