Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
fs.readFile and variable passing in node

I have two variables like this in my node project

header ="Hello Header";
result =  `<html>
    </head>
        <body>
            <div class='modal'>
                <div class='header'>
                    ${header}
                </div>
            </div>
        </body>
        </html>`;

I want to load this html part from an html file and pass the header value on to it. How we can achieve it ?

I know we can use fs.readFile

fs.readFile("index.html", 'utf8', function (err, data) {
    result = data;
});

But how can I pass the header variable into this ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The easiest and fastest solution would be to:

  • make the replaceable header part in the HTML discoverable with a pattern like {{ header }}

  • read the file as you would normally

    fs.readFile("index.html", 'utf8', function (err, data) { // make something here });

OR

you could use promises for better readability and compact code

import {promises} from "fs"
(async () => {
  const html = await promises.readFile(file, "utf8");
})()
  • then when you have the HTML in a variable just do:

    data.replace("{{ header }}", header)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda