且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

net :: ERR_CONNECTION_REFUSED错误jQuery ajax node.js

更新时间:2022-10-15 18:47:12

问题已解决。这是由于在pm2中使用手表标志而导致的。我的应用程序打印日志语句,并且日志文件夹与应用程序文件夹处于同一级别,因此无论何时更新日志文件,pm2都将重新启动我的应用程序服务器。这就是为什么如果我每几秒钟连接我的应用程序服务器就会工作,因为服务器重启只需要一秒左右的时间。如果我非常快速地连接服务器,它会给连接拒绝错误,因为服务器仍在重新启动,这就是连接被拒绝的原因。


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

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.