user1957076
user1957076

Reputation: 123

port error in node.js Error: connect ECONNREFUSED

I am trying to run an application on a local host but constantly get the following error:

Error: connect ECONNREFUSED
 at Object.exports._errnoException (util.js:1022:11)
 at exports._exceptionWithHostPort (util.js:1045:20)
 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)

I have installed node.js and did cmd commands: npm install and node index. What am I doing wrong? My code is as follow for the index file:

var config = require('./config');

// ******************* WebSocket server ******************* 

var wsClients = {};
var WebSocket = require('ws');
var webSocketServer = new WebSocket.Server({ 'host': config.webSocketHost,'port': config.webSocketPort }, function(){
    console.log('WebSocket listening '+config.webSocketHost+' on port '+config.webSocketPort+'.');
});

webSocketServer.on('connection', function(cws) {
    var id = Math.random();
    wsClients[id] = cws;
    console.log("New connection " + id);

    cws.on('close', function() {
        console.log('Connection close ' + id);
        delete wsClients[id];
    });

    cws.on('error', function() {
        console.log('Connection error ' + id);
        delete wsClients[id];
    });
});

webSocketServer.on('message', function(data){
    console.log(data);
});

// ******************* Web server *******************

var express = require('express');
var app = express();
app.use(express.static('public'));

app.set('views', './views');
app.set('view engine', 'ejs');
app.disable('x-powered-by');

app.get('/', function (req, res) {
    res.render('index');
});

app.use(function(req, res, next) {
    res.status(404).send('Sorry cant find that!');
});

app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

var host = config.webHost || process.env.HOST || "127.0.0.1";
var port = config.webPort || process.env.PORT || 80;

app.listen(port, host, function () {
    console.log('WebServer listening '+config.webHost+' on port '+config.webPort+'.');
});

// ******************* Twitter bot *******************

var Twitter = require('twitter'); 
var client = new Twitter(config);
var stream = client.stream('user');

stream.on('data', function(event, webSocketServer) {
    for (var key in wsClients) {
        wsClients[key].send(JSON.stringify(event));
    }
}); 

stream.on('error', function(error) {
    throw error;
});

Upvotes: 0

Views: 7275

Answers (1)

Santanu Biswas
Santanu Biswas

Reputation: 4787

ECONNREFUSED means 'The server refused connection'. This could be due to multiple reasons.

Run your server in debug mode, set a breakpoint on it and check if the connection request reaches the server application.

If the connection request does not reach the server application, check local firewall, network etc.

If it is reaching the server application and hits the breakpoint, run step by step and find which line of code is actually closing the connection.

Upvotes: 1

Related Questions