Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!