If I need to use nodejs to request multiple java interfaces and finally get all the data to render the page, how can I improve the speed?
This is what I did before
//请求接口
http.tp(options,function(error,response,tags){
http.tp(options1,function(error,response,topic){
http.tp(follow,function(error,response,follow){
http.tp(options2,function(error,response,topicRCMD){
data={
follow:follow,
topicRCMD:topicRCMD,
tags:tags,
topicHot:topic,
}
opt.render(data); //渲染页面
})
})
})
})
This is very slow. Then I quoted async
return async.parallel({
//我关注的人
follow:function(callback){
http.tp(follow,function(error, response, follow){
callback(null, follow);
})
},
// 获取标签
tags:function(callback){
http.tp(options,function(error, response, tags){
callback(null, tags);
})
},
// 获取热门话题
topicHot:function(callback){
http.tp(options1,function(error, response, topicHot){
callback(null, topicHot);
})
},
// 获取推荐话题列表
topicRCMD:function(callback){
http.tp(options2,function(error, response, topicRCMD){
callback(null, topicRCMD);
})
}
},
function(err, results){
console.log('ssss:',results);
opt.render(results); //渲染页面
});
This makes the speed a bit faster, but it is still very slow. Is there any good solution to this?
ringa_lee2017-05-16 13:46:59
The first method is slow because the time it takes is the sum of all request times; the second method only takes the time of the longest request, which will naturally be faster
If you want to optimize it, you need to do it in the rendering process or The server-side request processing process has been optimized
PHP中文网2017-05-16 13:46:59
You can only use something like promise.all. If there is any good method, please tell me.
给我你的怀抱2017-05-16 13:46:59
This problem is actually a rendering strategy problem. In fact, there is no need to complete all data requests in Node before sending them to the client.
You can completely put this step of requesting data on the client, and then the client uses ajax to obtain the data. This eliminates the need to wait for all data to be obtained.
巴扎黑2017-05-16 13:46:59
You can use the new promise in es6 syntax to specifically solve your callback black hole situation.
大家讲道理2017-05-16 13:46:59
I feel like I need to consider two points. First, do you rely on the previous one for everything? Is it possible to send some requests in parallel? Secondly, is it possible to distinguish the priorities and let the client send requests? After all, the client window is only so big, and the relationship between priorities will be clear. When the client sends requests, the important ones are sent first. Users will see it when loading the first screen, and then post the following;