因为我的应用用到了国外的API,小程序方不允许通过,我就用leancloud的云引擎做了一次中转,原本的API访问返回时间为1.16s。用了云引擎访问,获取返回值,然后我在小程序中调用云函数后,返回时间为10s+。因为原因是什么?
我访问的API为 https://traffic.krashnz.com/api/v2/public/server/ets2/eu2/traffic.json2
云函数用的在线编写格式为
return new Promise(function(resolve,reject){
var nodeRequest = require('request');
var trafficUrlArray = [
"https://traffic.krashnz.com/api/v2/public/server/ets2/eu1/traffic.json",
"https://traffic.krashnz.com/api/v2/public/server/ets2/eu2/traffic.json",
"https://traffic.krashnz.com/api/v2/public/server/ets2/eu3/traffic.json",
"https://traffic.krashnz.com/api/v2/public/server/ets2/eu4/traffic.json",
"https://traffic.krashnz.com/api/v2/public/server/ets2/eu5/traffic.json"
];
var serverId = request.params.id;
var url = trafficUrlArray[serverId];
var option ={
url: url,
method: "GET",
json: true,
headers: {
"content-type": "application/json",
},
}
nodeRequest(option, function(error, response, body) {
if (!error && response.statusCode == 200) {
resolve(body);
}
});
});