Does Node.js handle the client requests one by one? I have a project, which is using Node.js as a server-side proxy. According to my understanding, if you use a callback for a response, Node.js should respond to the new request without any delay, but actually the Node.js won't respond to the new request until it has finished the last callback. Is this the correct action or is there some incorrect code usage? Please help me on this. Thank you very much. Below is the code for re-sending requests to the back-end service.
var request = require('request');
var http = require('http');
function apiService(){}
apiService.prototype.get = function (context, payload, callback, url){
return this.requestAPI(context, payload, callback, url, "GET");
}
apiService.prototype.post = function(context, payload, callback, url){
return this.requestAPI(context, payload, callback, url, "POST");
}
apiService.prototype.del = function(context, payload, callback, url){
return this.requestAPI(context, payload, callback, url, "DELETE");
}
apiService.prototype.requestAPI = function(context, payload, callback, url, method){
var config = context.config;
var targetUrl = config.APIHost
+ (config.port == null ? "": (":" + config.port))
+ "/"
+ config.baseDir
+ "/"
+ url;
var requestObj = {
url : targetUrl,
json : true,
pool: new http.Agent()
}
if (config.proxy != null){
requestObj.proxy = config.proxy;
}
switch (method){
case "POST":
requestObj.body = payload;
request.post(requestObj, function (err, resp, body){
if (err){
callback(err);
return;
}
callback(null, body);
});
break;
case "GET":
var queryString = "";
for (att in payload){
if (queryString != "")
queryString += "&";
queryString += att.toString() + "=" + payload[att];
}
if (queryString!="")
requestObj.url += "?" + queryString;
request.get(requestObj, function (err, resp, body){
if (err){
callback(err);
return;
}
callback(null, body);
});
break;
case "DELETE":
requestObj.body = payload;
request.del(requestObj, function (err, resp, body){
if (err){
callback(err);
return;
}
callback(null, body);
});
break;
}
}
Current process
client request1 -> Node
client request2 -> Node
Node server request1 ->backend
Node (waiting for event loop)
Node <- server response1 backend
client <- response1 Node
Node server request2 ->backend
Node <- server response2 backend
client <- response2 Node
What I think it should be
client request1 -> Node
client request2 -> Node
Node server request1 -> backend
Node (won't waiting for event loop)
Node server request2 -> backend
Node <- server response2 backend
client <-response2 Node
Node <- server response1 backend
client <-response1 Node
Updated:
var params = {
action : 'list'
};
$http.post('../../service/buildingBlockService', params)
.success(function(buildingBlocks){
callback(null, buildingBlocks);
}).error(function(error){
callback(error);
})
The client requests are from Angular $http. On one page, there are several requests at the same time. According to my Fiddler monitor, the requests from the browser are sent without waiting, but the server re-send is one by one.
app.post('/service/buildingBlockService', function (ctx, payload, req, res) {
var service1 = require('./service/buildingBlockService');
var service = new service1();
service.service(ctx, payload, function(error, result){
res.send(200, result);
});
});
Updated: The middle layer of server request.
var service = require('./apiService');
function BuildingBlockService(){
}
BuildingBlockService.prototype.init = function(){}
BuildingBlockService.prototype.service = function(context, payload, callback) {
var apiService = new service()
var params = payload;
switch (params.action){
case "list":
default:
apiService.get(context, null, callback, "BuildingBlocks");
break;
}
};
module.exports = BuildingBlockService;