0

I am using HTML file and Express using NODEJS

exports.sendDashboardHtml = function(req,res,next){
  res.sendfile(HTMLLOCATION+'/dashboard.html');
};

which is delivering the HTML File with no problem. Now i want to add common header and footer file into this. How it possible. I don't using any template engine.

Thanks in advance

  • Well the easiest would be to use a view(template) engine. There are many to choose from and it makes your life easier. But if you want to do it yourself simply read the files you need using `fs.readFile` and join the text as you need. – Molda Jul 09 '20 at 11:23
  • Do we have any other option.. – sunny jindal Jul 09 '20 at 11:32
  • I believe you are referring html header and footer.. Then what about combining all together into a single file and serving that file ?@sunnyjindal – Jithin Scaria Jul 09 '20 at 12:13
  • How to combine into a single file, if i need to change header in future then i need to change it in all files – sunny jindal Jul 09 '20 at 12:46
  • Don't try to reinvent the wheel :) View engine is exactly the tool you want. It can combine the templates, it can insert a variables anywhere in the templates. It can combine the templates conditionaly, it can do a lot. – Molda Jul 09 '20 at 20:20
  • @Molda Can you give me one example for the same. i am using Pure HTML and jQuery as frontend – sunny jindal Jul 10 '20 at 03:06
  • The view engines for express https://expressjs.com/en/resources/template-engines.html each of them provide some examples as well as documentation. Have a look around a choose one. I personally like handlebars https://github.com/ericf/express-handlebars Also you can use any frontend framework, the engines will not affect your frontend. – Molda Jul 10 '20 at 08:58

0 Answers0