Ad

Net::ERR_CONNECTION_REFUSED Error JQuery Ajax Node.js

- 1 answer

This error happens when the date change in the date input happens very quickly. If I change the date every 2+ seconds, it works fine. But it gives the following error when the date input is changed very fast. In short, this error happens only when the next request is made right after the first one very quickly. I've spent hours and search a lot of similar issue with this error ERR_CONNECTION_REFUSED on SO and google, but no luck. Any ideas of what's causing this issue?

Also, when load the url (http://localhost:8000/svc/diary/2016-01-03) in browser, the data comes out fine, but if I reload (Ctrl + R) the url very fast, it goes to yahoo search with these terms:

http localhost 8000 svc diary 2016 01 03 diary

The Error Message from the console:

    GET http://localhost:8000/svc/diary/2016-01-03 net::ERR_CONNECTION_REFUSED
      send @ jquery-2.2.0.js:9172
      jQuery.extend.ajax @ jquery-2.2.0.js:8653
      (anonymous function) @ idiary.js:18
      jQuery.event.dispatch @ jquery-2.2.0.js:4732
      elemData.handle @ jquery-2.2.0.js:4544

The Date form

<input class="form-control" id='ddate' type="date" name="bday">


The Ajax Call

$('#ddate').on('change', function() {
    var ajaxParams = {
        url: '/svc/diary/' + $(this).val(),
        type: 'GET',
        contentType: "application/json",
        dataType:"json"
    };
    console.log(ajaxParams);
    $.ajax(ajaxParams)
        .done(function (data, textStatus, jqXHR) {
            console.log("diary retrieved!")
            console.log(data);
            $("#diary").text(data.diary);

        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            $("#diary").text("");
            console.log("failed to retrieve diary!");
            console.log(errorThrown);
        });
});


The backend node.js GET handler

function getDiary(req, res) {
    var date = req.params.date;
    util.log(mysql.format(searchQuery, [date]));
    util.dbpool.query(searchQuery, [date], function (err, result) {
        res.setHeader("Content-Type", "application/json");
        if (err) {
            console.log(err);
            util.errLog(JSON.stringify(err));
            res.status(500).json({message: 'summary, no results.'});
        } else {
            console.log(result);
            result.length > 0 ? res.status(200).json(result[0]) : res.status(200).json({"ddate":date, "diary":""});
        }
    });
}


UPDATE: Just found out, this issue is gone when starting the node application server with

node server.js

This issue happens only when starting the node application server with pm2 or foerver

pm2 start --watch server.js
forever start --watch server.js
Ad

Answer

The issue is resolved. It was caused by using the watch flag in pm2 and forever. My app prints log statements, and the log folder is in the same level as the application folder, so whenever a log file is updated, pm2 is restarting my application server. That is why it works if I pin my application server every few seconds because the server restart only takes about a second. It gives connection refused error if I pin the server very fast, because the server is still restarting, that's why it's connection refused.

Ad
source: stackoverflow.com
Ad