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

255
Visualizações
How to properly use response.end () in Node JS when reading a file asynchronously

I am to Node JS decided to create a simple routing where the file is read based on the url.

My problem is that I don't quite understand how to use response.end correctly.

This is my code without using response.end

http
  .createServer(function (req, res) {
    if (req.url === "/students") {
      fs.readFile("./templates/students.html", function (err, data) {
        if (!err) {
          res.write(data);
        }
      });
    } else {
      res.write('Page not found.')
    }
  })
  .listen(3000);

When I apply response.end inside a file read, then the else condition with an empty url stops working (as expected)

enter image description here

Otherwise, only the else condition works, and reading the file does not work when added at the end of url /students

enter image description here

This has always been a problem for me, I don’t understand how to use response.end correctly. It’s always confused me. How can I solve this problem?

about 4 years ago · Juan Pablo Isaza
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