【问题标题】:An better way to include head/footer with node/express?在节点/快递中包含页眉/页脚的更好方法?
【发布时间】:2023-03-30 08:08:01
【问题描述】:

当我们使用 fs.write 而不是 fs.render 并希望在输出中包含页眉和页脚时。我尝试使用包含,它给出了一个错误。

这样的行不起作用-> const head = require('../views/partials/production-header.html');

async function readInFile(theFile) {
  return await fs.readFile(path + theFile)
}

router.get('/:qry', function(req, res) {

  readInFile('/views/partials/production-header.html').then(data => {
    let head = data.toString()
    readInFile('/views/partials/production-footer.html').then(data => {
      let foot = data.toString()


        display_page(head, foot)
      })
    })
 })

 function display_page(head,foot,filter) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(head);
    res.write(q);
    res.write(foot);
    res.end();
 }

上述方法可行,但似乎不是最好的方法

【问题讨论】:

  • 为什么不使用模板引擎

标签: node.js express fs-extra


【解决方案1】:

有很多模板引擎,而且真的很容易使用,例如pug,你可以找到一个集成教程与快递here

【讨论】:

  • 感谢该链接很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多