包含 head/footer 和 node/express 的更好方法?

An better way to include head/footer with node/express?

在我们使用 fs.write 而不是 fs.render 并希望在输出中包含页眉和页脚的情况下。我尝试使用 include,但出现错误。

Lines like this do not work -> 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();
 }

以上方法有效,但似乎不是最好的方法

有很多模板引擎,而且非常容易使用,例如pug and you can find an integration tutorial with express here