Will I figure out how to add boilerplate HTML to static HTML files before close?
4
35
100
resolved May 7
Resolved
YES

My website is a plain HTML website, using Express to serve files. I want to have a single file that includes some boilerplate HTML, which then gets inserted into each HTML file that gets served, similar to a PHP include statement.

You'd this think would be simple, but no. I've spent hours on this and was unable to find a single tutorial or StackOverflow answer that addresses this. It's always "here's how to set up an entirely new website with EJS/Handlebars/Pug, no I'm not going to explain how to adapt an existing website to use them without changing anything else" or "write custom middleware for Express that allows you to perform arbitrary string replacements, no I'm not going to explain how to do that". And of course EJS appears to just have no official documentation whatsoever.

I think independent blogs have gotten so rare that nobody expects this is a thing anyone is trying to do anymore. Plz help.

I would accept any of:
Custom middleware for Express that lets me perform a String.replace() call on any HTML files served, without affecting other files.
A way to use EJS or another templating engine that lets me put something like "<include /templates/header>" into my HTML pages, and then when the file is served to the client, that line is replaced with the content of /templates/header.
Anything else that lets me do what I want in a reasonable way.

Get Ṁ600 play money

🏅 Top traders

#NameTotal profit
1Ṁ43
2Ṁ5
Sort by:

I think I figured out a terrible hacky solution; modify the npm serve-static package to intercept the request and test if the url points to an HTML file; if so, write a new file to disk and change the read path to point to that file.

I feel like this probably breaks something important, but I haven't run into any consequences yet...

If you're just using static HTML then maybe ditch Express and use Apache with mod include to do the thing you want.

@Odoacre Unfortunately I need a programmable web server for a few widgets that need AJAX support.

@IsaacKing you can still run them both and proxy the non html stuff through to Express

@Odoacre I believe you but I have no idea how to do that. Setting up my server to serve multiple domains and handle HTTPS was already hard enough, so given my lack of understanding of how non-Node webservers work and my desire to not break anything that I won't be able to fix, I'm gonna try my stupid idea above instead.