Inject meta tag dynamically to HTML with Express Inject meta tag dynamically to HTML with Express express express

Inject meta tag dynamically to HTML with Express


EJS would probably be the simplest one for that and very similar to PHP.

You can also take a look at Mustache and Handlebars for other options with minimal changes to your existing HTML.

  • with EJS: <html><head><%= yourMetaTags %> ...
  • with Mustache: <html><head>{{ yourMetaTags }} ...
  • with Handlebars: <html><head>{{ yourMetaTags }} ...

Also doT.js is very fast.

See:

Parsing the HTML and manipulating it with a DOM API just to insert meta tags would be an overkill in my opinion.

On the other hand if all you need is to insert meta tags then you could make a simple regex substitution, using something like yourHTML.replace('<head>', '<head>'+yourMetaTags); but it could potentially get more complex over time when you need more functionality. After all, everyone has made a templating engine at some point in life.