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

193
Vistas
why cant I get http.request to work on node.js?

Hi,

I am working on my app.js file on node.js and I want to make a http request to get the contents from an URL so I can use it as a string.

const app = require("express")();
const httpServer = require("http").createServer(app);
const io = require("socket.io")(httpServer);

app.get('/myappt', (req, res) => {
  res.sendFile('index.html');
});

let numUsers = 0;
    
var options = {
 host: 'http://example.net',
 path: '/test.txt',
 port: 80
};

callback = function(response) {
 var str = '';

 //another chunk of data has been received, so append it to `str`
 response.on('data', function (chunk) {
   str += chunk;
 });

 //the whole response has been received, so we just print it out here
 response.on('end', function () {
   username = str;
 });

};

httpserver.request(options, callback).end();
 
io.on('connection', (socket) => {
  let addedUser = false;

  // when the client emits 'add user', this listens and executes
  socket.on('add user', (username) => {
    if (addedUser) return;
    
    // we store the username in the socket session for this client
    socket.username = username;

its not working. The whole app will stop working at all and the only thing I get on the console is 500 errors. What is that?

Thank you.

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