Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js Error: connect ECONNREFUSED; response from server

I have a problem with this little program:

var http = require("http"); var request = http.request({     hostname: "localhost",     port: 8000,     path: "/",     method: "GET" }, function(response) {     var statusCode = response.statusCode;     var headers = response.headers;     var statusLine = "HTTP/" + response.httpVersion + " " +statusCode + " " + http.STATUS_CODES[statusCode];     console.log(statusLine);     for (header in headers) {         console.log(header + ": " + headers[header]);     }     console.log();     response.setEncoding("utf8");     response.on("data", function(data) {         process.stdout.write(data);     });     response.on("end", function() {         console.log();     }); }); 

The result in console is this:

 events.js:141       throw er; // Unhandled 'error' event       ^  Error: connect ECONNREFUSED 127.0.0.1:8000     at Object.exports._errnoException (util.js:870:11)     at exports._exceptionWithHostPort (util.js:893:20)     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)

I do not understand why this happens.

like image 609
Marco Ghieri Avatar asked Feb 04 '16 11:02

Marco Ghieri


People also ask

What is error connect Econnrefused?

The ECONNREFUSED Connection refused by the server error, is a common error returned by the Filezilla FTP client. This error indicates that the user is trying to connect to your server and is unable to connect to the port.


2 Answers

From your code, It looks like your file contains code that makes get request to localhost (127.0.0.1:8000).

The problem might be you have not created server on your local machine which listens to port 8000.

For that you have to set up server on localhost which can serve your request.

  1. Create server.js

    var express = require('express'); var app = express();  app.get('/', function (req, res) {   res.send('Hello World!'); // This will serve your request to '/'. });  app.listen(8000, function () {   console.log('Example app listening on port 8000!');  }); 
  2. Run server.js : node server.js

  3. Run file that contains code to make request.

like image 137
Piyush Sagar Avatar answered Sep 21 '22 21:09

Piyush Sagar


Please use [::1] instead of localhost, and make sure that the port is correct, and put the port inside the link.

const request = require('request');     let json = {         "id": id,         "filename": filename     };     let options = {         uri: "http://[::1]:8000" + constants.PATH_TO_API,         // port:443,         method: 'POST',         json: json     };     request(options, function (error, response, body) {         if (error) {             console.error("httpRequests : error " + error);         }         if (response) {             let statusCode = response.status_code;             if (callback) {                 callback(body);             }         }     }); 
like image 24
Ahmad Zahabi Avatar answered Sep 22 '22 21:09

Ahmad Zahabi