res.render,res.write和Jade

时间:2013-03-30 12:48:05

标签: javascript node.js express pug

我正在尝试在Node.js中创建简单的ftp客户端。一切正常,但我不知道如何在我的Jade模板中写“singleFile.name”。

app.post('/ftp/login', function(req, res){
  ftp.ls("/directory", function(err, files){
      if (err) return console.error(err);

      files.forEach(function(singleFile) {
        if (singleFile != null){
        console.log(singleFile.name + "<br>");
        }
      });
  });

 res.render('ftpLogin', { host: fHost, username: fUsername, port: fPort});
});

没有“res.render”没有问题,但我想将它添加到我的模板中。

res.writeHead(200, {"Content-type" : "text/html; charset=utf-8"});
[...]
res.write(singleFile.name + "<br>");
[...]
res.end();

我是新手(3天节点学习),所以我很乐意回答你所有的错误。

谢谢!

1 个答案:

答案 0 :(得分:0)

最好将文件数组发送到Jade模板。

res.render('ftpLogin', { host: fHost, ..., files: files });

然后在模板中编写一个循环。例如:

each file in files
    | #{file.name}
    br

请参阅Jade循环文档:https://github.com/visionmedia/jade#a9

尝试使用+ "<br>"之类的代码“预格式化”代码并不是一种好的做法,因为它本身就是模板引擎的重点。