nodejs發(fā)送http請(qǐng)求時(shí)遇到404長(zhǎng)時(shí)間未響應(yīng)的解決方法
通常,我們?cè)谑褂胣odejs發(fā)送http請(qǐng)求時(shí),一旦遇到404響應(yīng),nodejs內(nèi)部會(huì)一直請(qǐng)求下去,直到超出它自己設(shè)定的響應(yīng)時(shí)長(zhǎng)(最讓人惡心的地方就是這個(gè)時(shí)長(zhǎng)還是沒(méi)法修改的。)很多人在這里碰到了麻煩。
我是在做arcgis地圖項(xiàng)目的時(shí)候,客戶(hù)提出需要使用天地圖提供的底圖服務(wù),當(dāng)時(shí)我直接使用silverlight客戶(hù)端的Arcgis API進(jìn)行http請(qǐng)求(同樣是內(nèi)部請(qǐng)求,不開(kāi)源的東西就是這么讓人郁悶),同樣碰到了一個(gè)進(jìn)度條一直卡在那的問(wèn)題。經(jīng)過(guò)調(diào)試發(fā)現(xiàn),是由于底圖加載請(qǐng)求超時(shí)的緣故,和nodejs一樣,silverlight一直在進(jìn)行請(qǐng)求直到超出它自己設(shè)定的響應(yīng)時(shí)限。
于是,我當(dāng)時(shí)正好有業(yè)余接觸nodejs,覺(jué)得這個(gè)東西性能應(yīng)該是不錯(cuò)的,至少比tomcat+java之流要好一些。于是,我著手寫(xiě)了一個(gè)nodejs的代理服務(wù),用來(lái)請(qǐng)求天地圖的底圖。我當(dāng)時(shí)以為nodejs碰到404時(shí)能直接結(jié)束請(qǐng)求,但是呢,這個(gè)問(wèn)題好像是行業(yè)規(guī)范似的,它竟然也和silverlight一樣不斷的請(qǐng)求……索性上網(wǎng)查了會(huì)資料,得出了以下這兩段代碼,解決了這個(gè)一直請(qǐng)求404的問(wèn)題。
function proxyTDTMapData(img,level,row,col){
var that = this,request = null,param = img.replace('_w','');
var filename = tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png';
path.exists(filename, function(exists) {
if (exists) {
readFileEntry(filename,that.res);
}else{
var url = "http://t0.tianditu.com/"+img+"/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=" + param + "&tileMatrixSet=w&TileRow=" + row + "&TileCol=" + col + "&TileMatrix=" + level + "&style=default&format=tiles";
httpGetWithTimeoutSupport(url,4000,function(response){
//console.log("have a response!");
if(200 == response.statusCode){
var size = 0;
var chunks = [];
response.on('data', function(chunk){
size += chunk.length;
chunks.push(chunk);
});
response.on('end', function(){
var data = Buffer.concat(chunks, size);
that.res.writeHead(200, {
'Content-Type' : 'image/png',
'Content-Length' : data.length,
'Accept-Ranges' : 'bytes',
'Server' : 'Microsoft-IIS/7.5',
'X-Powered-By' : 'ASP.NET'
});
that.res.write(data, "binary");
that.res.end();
fs.writeFile(tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png', data);
});
}else{
readFileEntry(mapDir+"/null.png",that.res);
}
}).on("error",function(){
readFileEntry(mapDir+"/null.png",that.res);
});
}
});
}
function httpGetWithTimeoutSupport(options, timeout, callback) {
var timeoutEvent;
var req = http.get(options, function(res) {
res.on("end", function() {
clearTimeout(timeoutEvent);
// console.log("end");
})
res.on("close", function(e) {
clearTimeout(timeoutEvent);
// console.log("close");
})
res.on("abort", function() {
// console.log("abort");
});
res.on("error",function(){
try{
res.destory();
clearTimeout(timeoutEvent);
//console.log("res error catch");
}catch(e){
}
});
callback(res);
});
req.on("timeout", function() {
//console.log("request emit timeout received");
try{
if (req.res) {
req.res.emit("abort");
}
clearTimeout(timeoutEvent);
req.abort();
}catch(e){
//console.log("req timeout failed!");
}
});
req.on("error",function(){
try{
//console.log("req error catch");
}catch(e){
}
});
timeoutEvent = setTimeout(function() {
try{
req.emit("timeout");
}catch(e){
//console.log("timeout failed!");
}
}, timeout);
return req;
}
其原理就是利用nodejs請(qǐng)求的幾個(gè)事件與計(jì)時(shí)器,一旦超出設(shè)定的響應(yīng)時(shí)長(zhǎng)則立馬終結(jié)請(qǐng)求。如此,進(jìn)度條一直卡著的問(wèn)題解決了。
細(xì)心的讀者可能看到了
path.exists(filename, function(exists) {
if (exists) {
readFileEntry(filename,that.res);
}else{...});
這段代碼,其實(shí)這里做了一下服務(wù)端的圖片緩存,一旦加載過(guò)的底圖圖片,直接從本地讀取,極大的加快了地圖的訪問(wèn)速度(這個(gè)在效率上提升了至少10倍)。
至于Arcgis API for Silverlight 是如何實(shí)現(xiàn)天地圖底圖以及其它底圖服務(wù)(比如非標(biāo)準(zhǔn)墨卡托的地方坐標(biāo)系底圖服務(wù))加載的呢?請(qǐng)聽(tīng)我下回分解。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Node.js實(shí)現(xiàn)前端后端數(shù)據(jù)傳輸加密解密
這篇文章主要介紹了Node.js實(shí)現(xiàn)前端后端數(shù)據(jù)傳輸加密解密,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
node事件循環(huán)和process模塊實(shí)例分析
這篇文章主要介紹了node事件循環(huán)和process模塊,結(jié)合實(shí)例形式分析了node事件循環(huán)和process模塊具體功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
詳解使用Typescript開(kāi)發(fā)node.js項(xiàng)目(簡(jiǎn)單的環(huán)境配置)
本篇文章主要介紹了詳解使用Typescript開(kāi)發(fā)node.js項(xiàng)目(簡(jiǎn)單的環(huán)境配置),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
如何讓Nodejs支持H5 History模式(connect-history-api-fallback源碼分析)
這篇文章主要介紹了如何讓Nodejs支持H5 History模式(connect-history-api-fallback源碼分析),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05

