将正则表达式数据呈现给Jade

时间:2014-06-26 14:41:53

标签: javascript regex node.js pug

因此,在下面的代码中,我将从正则表达式匹配中提取的数据写入控制台。但是,我需要将此数据呈现给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])]
}    
}

更新:所以这是我到目前为止所尝试的。

玉文件:

...
  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;
}

但它会呈现一个错误,即日志未定义。我想是因为它没有将匹配项放入日志条目中。

1 个答案:

答案 0 :(得分:0)

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中。

相关问题