玉文件:
form(method="post", action="/upload", enctype="multipart/form-data")
input(type="file", name="logName")
input#promptNumErrors(type="number", name="numErr", placeholder="Number of Errors")
button(type="submit") Upload
Index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res) {
res.render('fileUpload', { title: 'Log File Viewer'});
});
var formidable = require('formidable'),
fs = require('fs'),
util = require('util'); //Used to print out everything about the log file
/* POST the file upload */
router.post('/upload', function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
fs.readFile(files.logName.path, function (err, data) {
if (err) throw err;
// TODO: Getting form value
var numError = req.body.numErr;
console.log(numError);
var reDiaBtoa = /^(\[[^\]]*\]) (\w+?) (\[[^\]]*\]) (\([^)]*\)) (.*)$/gm;
var outputToDisplay = " ";
// Check to see if regex string works
if (reDiaBtoa.test(data)) {
var array = data.toString().split('\n');
for (l in array) {
var logArray = (array[l]);
}
// Use numPrompt to retrieve user input for numErr
var numPrompt = 10;
// Reverse the array so the most recent errors will show
var newArray = array.reverse();
var match;
//Count the number of errors in the log file
var countErrors=0;
var numCount = (countErrors-numPrompt);
for (var j = -1; j <= newArray.length; j++) { // Since newArray[0] contains an empty string
while ((match = reDiaBtoa.exec(newArray[j])) != null) {
if (match.index === reDiaBtoa.lastIndex) {
reDiaBtoa.lastIndex++;
}
if (match[2] === "ERROR") {
countErrors++;
res.write('<p>' + "Time " + match[1] + '<br/>');
res.write("Thread Name: " + match[3] + '<br/>');
res.write("Source name & line number: " + match[4] + '<br/>');
res.write("Log Message: " + match[5] + '<br/>');
res.write('--------------------------------------' + '</p>');
}
}
}
console.log(countErrors);
}
})
});
});
每当我运行程序时,它一直说numError是未定义的。我能够读取文件输入,但不能读取数字输入。有什么想法吗?
更新:这是完整的index.js文件。