javascript - Retrieving form data Node.js -
jade file:
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(); /* 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 print out log file /* post 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 see if regex string works if (rediabtoa.test(data)) { var array = data.tostring().split('\n'); (l in array) { var logarray = (array[l]); } // use numprompt retrieve user input numerr var numprompt = 10; // reverse array recent errors show var newarray = array.reverse(); var match; //count number of errors in log file var counterrors=0; var numcount = (counterrors-numprompt); (var j = -1; j <= newarray.length; j++) { // since newarray[0] contains 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); } }) }); });
it keeps saying numerror undefined whenever run through program. able read file input, not number input. ideas?
update: full index.js file.
Comments
Post a Comment