Saturday 11 March 2017

Http Request can't send a response immediately in Nodejs server

I send JSON requests one by one to the nodejs server. After 6th request, server can't reply to the client immediately and then it takes a little while(15 seconds or little bit more and send back to me answer 200 ok) It occurs a writing json value into MongoDB and time is important option for me in terms with REST call. How can I find the error in this case? (which tool or script code can help me?) My server side code is like that

var controlPathDatabaseSave = "/save";
app.use('/', function(req, res) {
console.log("req body app use", req.body);
var str= req.path;

if(str.localeCompare(controlPathDatabaseSave) == 0)
{
    console.log("controlPathDatabaseSave");
    mongoDbHandleSave(req.body);
    res.setHeader('Content-Type', 'application/json');
    res.write('Message taken: \n');
    res.write('Everything all right with database saving');
    res.send("OK");
    console.log("response body", res.body);
}

});

My client side code as below:

function saveDatabaseData()
{
    console.log("saveDatabaseData");
        var oReq = new XMLHttpRequest();
        oReq.open("POST", "http://192.168.80.143:2800/save", true);
        oReq.setRequestHeader("Content-type", "application/json;charset=UTF-8");
        oReq.onreadystatechange = function() {//Call a function when the state changes.
            if(oReq.readyState == 4 && oReq.status == 200) {
                console.log("http responseText", oReq.responseText);
            }
        }
        oReq.send(JSON.stringify({links: links, nodes: nodes}));
}

You can see my REST call in google chrome developer editor. (First six call has 200 ok. Last one is in pending state)

enter image description here

Thanks in advance,



via zoint

No comments:

Post a Comment