我有这个node.js应用程序(使用socket.io),将用于监视生产环境中对网络配置更改所做的更改。它监视后台打印目录中是否有新文件,然后将信息推送到Web客户端。当我尝试运行它时,出现“错误:无法读取未定义的属性'length'。”任何帮助将不胜感激。
#!/usr/bin/env node
// Runtime configuration
const SPOOLDIR = process.env['HOME'] + "/spool"; // Where to collect files
const INTERVAL = 5000; // How often to scan
const GRACE = 2000; // Minimum age of file before processing
// Dependency modules
const fs = require("fs");
const os = require("os");
const util = require("util");
const app = require('http').createServer(handler)
const io = require('socket.io')(app);
// Global variables:
// - File cache: stat structures by filename
var CACHE={};
// Mini C STDIO printf/fprintf routines :)
//
const STDOUT=1;
const STDERR=2;
const fprintf = function(fd, fmt) {
utilfmt_args = Array.prototype.slice.call(arguments, 1);
var str = util.format.apply(null, utilfmt_args);
fs.writeSync(fd, str);
}
const printf = function() {
Array.prototype.unshift.call(arguments, STDOUT);
fprintf.apply(null, arguments);
}
const startFileScan = function() {
fs.readdir(SPOOLDIR, processFileResults);
}
const processFileResults = function(err, files) {
fprintf(STDERR, "processFileResult: %d file(s) found in %s\n", files.length, SPOOLDIR);
if (err!=undefined) {
fprintf(STDERR, "Can't read spool directory %s: %s\n", SPOOLDIR, err.code);
return;
}
// Expire any items from the cache that are no longer present
for (var f in CACHE) {
if (files.indexOf(f)==-1) {
// fprintf(STDERR, "Removing file %s from cache\n", f);
delete CACHE[f];
}
}
// Check any files that are there for modifications, processing them if so
var currentFile = undefined;
const doStat = function(err, stats) {
if (err) {
fprintf(STDERR, "Error stat()ing %s: %s\n", currentFile, err.code);
} else {
if (currentFile!==undefined) {
// fprintf(STDERR, "Checking file %s with mtime %s against cache\n", currentFile, stats.mtime);
if (!(currentFile in CACHE) || !(CACHE[currentFile].getTime()==stats.mtime.getTime())) {
if (stats.mtime.getTime() + GRACE < Date.now()) {
// fprintf(STDERR, " Updating cache for file %s with mtime %s\n", currentFile, stats.mtime);
CACHE[currentFile]=stats.mtime;
// fprintf(STDERR, " File %s has been modified longer than %d ms ago: scheduling for processing\n", currentFile, GRACE);
process.nextTick(outputDiffReport, currentFile, CACHE[currentFile]);
}
}
}
currentFile = files.pop();
if (currentFile===undefined) {
// fprintf(STDERR, "File scan completed: re-scheduling next scan\n");
process.nextTick(function() { setTimeout(startFileScan, INTERVAL); });
} else {
fs.stat(SPOOLDIR + "/" + currentFile, doStat);
}
};
process.nextTick(doStat);
}
// App library routines
//
const outputDiffReport = function(filename, mtime) {
// fprintf(STDERR, "Processing file %s\n", filename);
var data="";
try {
data = fs.readFileSync(SPOOLDIR + "/" + filename, { encoding: "utf8" });
} catch(err) {
fprintf(STDERR, "Can't read incoming filename %s: %s\n", filename, err.code);
}
content = data.split(/\n/).filter(function(x) { return x.match(/^[+\-#\[]/); }).join("\n");
// content = data.split(/\n/).filter(function(x) { return ! x.match(/^\s*$/); }).join("\n");
io.emit('update', { 'content': content,
'mtime': mtime.toISOString(),
'file': filename
});
fs.unlink(SPOOLDIR + "/" + filename, function() {});
}
// HTTP bootstrap handler routine. Serves out the client HTML
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
process.on('uncaughtException', function(err) {
console.log("ERROR: ", err.message);
process.exit(1);
});
app.listen(8080);
process.nextTick(startFileScan);
答案 0 :(得分:1)
调用%install
时,它要么会作为第一个参数出现错误,要么会成功并为您提供processFileResults
作为第一个参数,并给您第二个文件。
在尝试访问null
之前,您应该检查错误并进行处理。另外,从语义上检查错误是否为files
也是没有意义的,因为错误只能是undefined
或错误。
null
答案 1 :(得分:0)
原来是假脱机目录的权限问题。我创建的用于运行程序的用户对该程序没有读取权限。