As placed in comments, you can use regular expressions for this.
You can also, for example, edit your file and include some "special comment", which we will use to restore by the script to be injected. Something like that:
<!DOCTYPE html>
<html>
<head>
<title>Hello, world!</title>
<meta charset="UTF-8" />
<!-- inject::script -->
</head>
<body>
Hello, world!
</body>
</html>
You can then create a function to encapsulate this behavior:
function injectScript(file, script) {
return file.replace('<!-- inject::script -->', `<script>${script}</script>`);
}
And just pass the contents of the file:
const { promises: fs } = require('fs');
const path = require('path');
const app = require('express')();
const filePath = path.join(__dirname, 'template.html');
const script = `
window.addEventListener('load', () => {
document.body.style.color = 'rgb(255, 0, 0)';
});
`;
function injectScript(file, script) {
return file.replace('<!-- inject::script -->', `<script>${script}</script>`);
}
app.get('/', async (req, res) => {
try {
// Obter o HTML:
const file = await fs.readFile(filePath, 'utf8');
// Injetar o script:
const injected = injectScript(file, script);
// Enviar o HTML com o script injetado:
res.send(injected);
} catch (error) {
res.send(`Whoops. Erro: ${error.message}.`);
}
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening at port ${port}.`));
See working on Codesandbox.
Note that if you can’t edit the templates to add the comment, just use some tag who will always be in the <head>
as a point of reference instead of a comment. In my opinion, </head>
is the simplest to be used in this case.
Thus:
function injectScript(file, script) {
return file.replace('</head>', `<script>${script}</script></head>`);
}
Note in the example above that we exchange </head>
by a script tag and the closing of the </head>
right away.
You can also use the calls template Engines, that help you do this more easily. Nunjucks is the simplest in my opinion, with an inspired syntax of template engine jinja2 by Python.
<title>
, for example, and add the code after the term; 2) You can use template engine as haml, ejs, vuexpress etc..– Valdeir Psr