模块“require”在Jade文件中

时间:2014-11-25 16:01:48

标签: node.js pug

我需要在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

我收到错误“未定义不是函数”

2 个答案:

答案 0 :(得分:6)

两个问题

您应该将函数作为参数发送到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


此外,在第2行中,您调用函数“readdirSync”而不在任何地方定义它。它应该是

fs.readdirSync

答案 1 :(得分:2)

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

现在你的玉。

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
相关问题