Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda