On the occasion when we are using fs.write rather than fs.render and want to include a header and a footer with our output. I tried using include and it gives an error.
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();
}
the above works but hardly seems like the best way to go about it
There are a lot of template engines for that, and is really easy to use, for example pug and you can find an integration tutorial with express here