Node.js unhandled 'error' Event

Asked

Viewed 247 times

1

'use strict'

const http = require('http');


http.get({
    hostname: 'localhost',
    path: '/user?name=jv&jovem=true&agr=19',
    port: 3000,
    agent: false
}, function(response) {
    let body = "";
    console.log('STATUS:' + response.statusCode);
    console.log('HEADERS:' + response.headers);
    response.on('data', function (data) {
        body += data;
    });

    response.on('end', function () {
        console.log('Resposta:', body);
    });
});

when I rotate on the console the error appears:

unhandled 'error' event
Error: connect ECONNREFUSED IP:3000
at Object.exports._errnoException (util.js:1085:14)
at exports._exceptionWithHostPort (util.js:1049:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1085:14)
  • 1

    Forehead join .on('error', (e) => console.log(e)); to catch that mistake, put it at the end of your code, before ;

  • on the console appeared: code: 'enconnrefused', Errno: 'enconnrefused', syscall: 'connect' address: 'myIP', port: 3000

  • 1

    You have the server running on the localhost?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.