因此,在下面的代码中,我将从正则表达式匹配中提取的数据写入控制台。但是,我需要将此数据呈现到一个 Jade 文件中,以便上传日志文件的用户可以自己查看匹配项。有任何想法或网站提供信息吗?
while ((match = reDiaBtoa.exec(newArray[j])) != null) {
if (match.index === reDiaBtoa.lastIndex) {
reDiaBtoa.lastIndex++;
}
if (match[2] === "ERROR") {
console.log(match[2]);
console.log("Time " + match[1]);
console.log("Thread Name: " + match[3]);
console.log("Source name & line number: " + match[4]);
console.log("Log Message: " + match[5] + '\n');
console.log("-----------------------------------------------------------------" + "\n");
var logs = [new Log(match[1], match[3], match[4], match[5])]
}
}
更新:这就是我到目前为止所尝试过的。
Jade 文件:
...
form(method="post", action="/upload", enctype="multipart/form-data")
input(type="file", name="logName")
button(type="submit") Upload
h1.
Log List
#logs
for log in logs
include log
在我的index.js 文件中使用此代码
/* GET home page. */
router.get('/', function (req, res) {
res.render('fileUpload', { title: 'Building a Log File Viewer' }, {logs: logs});
});
function Log(time, thread, source, mess) {
this.time = time;
this.thread = thread;
this.source = source;
this.mess = mess;
}
但它会呈现一个日志未定义的错误。我想是因为它不会将匹配项放入日志条目中。
最佳答案
res.render('fileUpload', { title: 'Building a Log File Viewer' }, {logs: logs});
应该是
res.render('fileUpload', { title: 'Building a Log File Viewer', logs: logs });
我认为这将使logs
成为jade模板内的数组,但我不认为您的include log
会将log
视为变量,它只是尝试包含一个名为“log.jade”的文件。您可能需要读取 javascript 代码中的日志文件,以获取所需的字符串数据,然后将其传递给 jade 以包含在 HTML 中。
关于javascript - 将正则表达式数据渲染到 Jade,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433485/