Jade文件中的模块"require"

5

我需要在Jade文件中使用“fs”模块,不能使用其他JS文件。

当我尝试使用以下代码时:

- var js = require('fs')
- var downloadfiles = readdirSync("download") 
for f in downloadfiles
  url
    loc= D + "/download" + f
    lastmod= buildDate
    changefreq daily
    priority 1.0

我遇到了“undefined is not a function”错误。


2个回答

7

两个问题

你应该将函数作为参数发送到Jade文件中,而不是尝试在Jade中要求它。

var js = require('fs');
res.render('myjadefile', {fsFunction: fs});

myjadefile.jade

- var downloadfile = fsFunction.readdirSync('download');
for f in downloadfiles
   // rest of your code

此外,在第二行调用了函数“readdirSync”,但在任何地方都没有定义它。应该是:

fs.readdirSync

3
res.render(index, {data:data,
                   title:'Jade Require'
                   require:require})

现在进入您的Jade页面。
extends layout
!{fs=require('fs')}


body
  - var downloadfiles = readdirSync("download") 
  for f in downloadfiles
    url
      loc= D + "/download" + f
      lastmod= buildDate
      changefreq daily
      priority 1.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接