您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 淮安分类信息网,免费分类信息发布

在nodejs中遇到404长时间未响应

2024/3/20 12:52:12发布16次查看
这篇文章主要为大家详细介绍了nodejs发送http请求时遇到404长时间未响应的解决方法
通常,我们在使用nodejs发送http请求时,一旦遇到404响应,nodejs内部会一直请求下去,直到超出它自己设定的响应时长(最让人恶心的地方就是这个时长还是没法修改的。)很多人在这里碰到了麻烦。
我是在做arcgis地图项目的时候,客户提出需要使用天地图提供的底图服务,当时我直接使用silverlight客户端的arcgis api进行http请求(同样是内部请求,不开源的东西就是这么让人郁闷),同样碰到了一个进度条一直卡在那的问题。经过调试发现,是由于底图加载请求超时的缘故,和nodejs一样,silverlight一直在进行请求直到超出它自己设定的响应时限。
于是,我当时正好有业余接触nodejs,觉得这个东西性能应该是不错的,至少比tomcat+java之流要好一些。于是,我着手写了一个nodejs的代理服务,用来请求天地图的底图。我当时以为nodejs碰到404时能直接结束请求,但是呢,这个问题好像是行业规范似的,它竟然也和silverlight一样不断的请求……索性上网查了会资料,得出了以下这两段代码,解决了这个一直请求404的问题。
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请求的几个事件与计时器,一旦超出设定的响应时长则立马终结请求。如此,进度条一直卡着的问题解决了。
细心的读者可能看到了
path.exists(filename, function(exists) { if (exists) { readfileentry(filename,that.res); }else{...});
这段代码,其实这里做了一下服务端的图片缓存,一旦加载过的底图图片,直接从本地读取,极大的加快了地图的访问速度(这个在效率上提升了至少10倍)。
至于arcgis api for silverlight 是如何实现天地图底图以及其它底图服务(比如非标准墨卡托的地方坐标系底图服务)加载的呢?请听我下回分解。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
在vue-router中如何实现路由懒加载
在angular2中如何实现断点调试ts文件
如何实现网页快报向上滚动
以上就是在nodejs中遇到404长时间未响应的详细内容。
淮安分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录