0

当我们使用 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();
 }

以上工作,但似乎并不是最好的方法

4

1 回答 1

1

有很多模板引擎,并且非常易于使用,例如pug ,您可以在此处找到带有 express 的集成教程

于 2019-02-15T05:34:21.063 回答