2

This is my code in node js:

var http = require('http');

var options = { 
    host : '127.0.0.1',
    port: 8124,
    path: '/',
    method: 'GET'
};

http.request(options, function(res){    
    console.log("Hello!");  
}).end();

process.on('uncaughtException', function(err){  
    console.log(err);
});

When I compile it, the compiler shows me the following error:

enter image description here

edit: with the express works, but if I wanted to make it work without express how could I do?

6
  • Do you have port 8124 open? Check your firewall -- also run telnet to check: telnet 127.0.0.1 8124 Commented Jul 6, 2017 at 12:42
  • isn't a port problem Commented Jul 6, 2017 at 12:50
  • Your getting an econnrefused which is either the ports not open or the service is not listening on the port. You should confirm that the port is open by running telnet Commented Jul 6, 2017 at 12:50
  • even if I put random port number and run many times wrong Commented Jul 6, 2017 at 12:51
  • instead if I create the connection with http. create server working properly Commented Jul 6, 2017 at 12:52

2 Answers 2

1

Test this:

const app = require('express')();
app.get('/', (req, res) => {
  res.json({ ok: true });
});
app.listen(8124);

var http = require('http');

var options = {
    host : '127.0.0.1',
    port: 8124,
    path: '/',
    method: 'GET'
};

http.request(options, function(res){
    console.log("Hello!");
}).end();

process.on('uncaughtException', function(err){
    console.log(err);
});

as you can see, it prints Hello! - when something is listening on port 8124. Your problem is on the server side, not on the client side. Specifically, the server that you are trying to connect to is not listening on port 8124 on localhost - at least not on this host.

Sign up to request clarification or add additional context in comments.

Comments

0

To resolve this issue, it was enough to add to the previous code, the server code.

var http = require('http');

var options = { 
    host : '127.0.0.1',
    port: 8124,
    path: '/',
    method: 'GET'
};

http.request(options, function(res){    
    console.log("Hello!");  
}).end();

process.on('uncaughtException', function(err){  
    console.log(err);
});

http.createServer((req, res)=>{ 
    res.writeHead(200, {'Content-type': 'text/plain'})
    res.end()   
}).listen(8124)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.