简体   繁体   中英

Node.js custom Module

When I run app.js , I get this error:

MBPdiDaniele3:Desktop danielemartini$ node app.js 
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:8080
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
MBPdiDaniele3:Desktop danielemartini$ 

Here's the code for make_request.js :

var http = require('http');

var makeRequest = function(message) {

    var options = {
        host: 'localhost', port: 8080, path:'/', method: 'POST'
    }

    var request = http.request(options, function(response) {
        response.on('data', function(data) {
            console.log(data);
        });
    });
    request.write(message);
    request.end();
};
module.exports = makeRequest;

Here's the code for app.js :

var makeRequest = require('./make_request');

makeRequest("Here's looking at you, kid");
makeRequest("Hello, this is dog");

There are several possible causes :

  1. No service is running on localhost:8080
  2. A service runs on 8080 which refuses connections actively.

To check what is running, use one of those 2 commands (Unix) :

  • lsof -i :8080 -S
  • netstat -a | grep 8080

3 - Your running service isn't bound to your internal IP.

I've encountered the issue a few times on Cloud servers, where localhost/127.0.0.1 are not recognized. Try using the external IP of your machine (and make sure the firewall lets you make requests), or force your service to bind to all interfaces.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM