当前位置:Gxlcms > JavaScript > 在nodejs中遇到404长时间未响应

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

时间:2021-07-01 10:21:17 帮助过:5人阅读

这篇文章主要为大家详细介绍了nodejs发送http请求时遇到404长时间未响应的解决方法

通常,我们在使用nodejs发送http请求时,一旦遇到404响应,nodejs内部会一直请求下去,直到超出它自己设定的响应时长(最让人恶心的地方就是这个时长还是没法修改的。)很多人在这里碰到了麻烦。

我是在做arcgis地图项目的时候,客户提出需要使用天地图提供的底图服务,当时我直接使用silverlight客户端的Arcgis API进行http请求(同样是内部请求,不开源的东西就是这么让人郁闷),同样碰到了一个进度条一直卡在那的问题。经过调试发现,是由于底图加载请求超时的缘故,和nodejs一样,silverlight一直在进行请求直到超出它自己设定的响应时限。

于是,我当时正好有业余接触nodejs,觉得这个东西性能应该是不错的,至少比tomcat+java之流要好一些。于是,我着手写了一个nodejs的代理服务,用来请求天地图的底图。我当时以为nodejs碰到404时能直接结束请求,但是呢,这个问题好像是行业规范似的,它竟然也和silverlight一样不断的请求……索性上网查了会资料,得出了以下这两段代码,解决了这个一直请求404的问题。

  1. function proxyTDTMapData(img,level,row,col){
  2. var that = this,request = null,param = img.replace('_w','');
  3. var filename = tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png';
  4. path.exists(filename, function(exists) {
  5. if (exists) {
  6. readFileEntry(filename,that.res);
  7. }else{
  8. 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";
  9. httpGetWithTimeoutSupport(url,4000,function(response){
  10. //console.log("have a response!");
  11. if(200 == response.statusCode){
  12. var size = 0;
  13. var chunks = [];
  14. response.on('data', function(chunk){
  15. size += chunk.length;
  16. chunks.push(chunk);
  17. });
  18. response.on('end', function(){
  19. var data = Buffer.concat(chunks, size);
  20. that.res.writeHead(200, {
  21. 'Content-Type' : 'image/png',
  22. 'Content-Length' : data.length,
  23. 'Accept-Ranges' : 'bytes',
  24. 'Server' : 'Microsoft-IIS/7.5',
  25. 'X-Powered-By' : 'ASP.NET'
  26. });
  27. that.res.write(data, "binary");
  28. that.res.end();
  29. fs.writeFile(tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png', data);
  30. });
  31. }else{
  32. readFileEntry(mapDir+"/null.png",that.res);
  33. }
  34. }).on("error",function(){
  35. readFileEntry(mapDir+"/null.png",that.res);
  36. });
  37. }
  38. });
  39. }
  1. function httpGetWithTimeoutSupport(options, timeout, callback) {
  2. var timeoutEvent;
  3. var req = http.get(options, function(res) {
  4. res.on("end", function() {
  5. clearTimeout(timeoutEvent);
  6. // console.log("end");
  7. })
  8. res.on("close", function(e) {
  9. clearTimeout(timeoutEvent);
  10. // console.log("close");
  11. })
  12. res.on("abort", function() {
  13. // console.log("abort");
  14. });
  15. res.on("error",function(){
  16. try{
  17. res.destory();
  18. clearTimeout(timeoutEvent);
  19. //console.log("res error catch");
  20. }catch(e){
  21. }
  22. });
  23. callback(res);
  24. });
  25. req.on("timeout", function() {
  26. //console.log("request emit timeout received");
  27. try{
  28. if (req.res) {
  29. req.res.emit("abort");
  30. }
  31. clearTimeout(timeoutEvent);
  32. req.abort();
  33. }catch(e){
  34. //console.log("req timeout failed!");
  35. }
  36. });
  37. req.on("error",function(){
  38. try{
  39. //console.log("req error catch");
  40. }catch(e){
  41. }
  42. });
  43. timeoutEvent = setTimeout(function() {
  44. try{
  45. req.emit("timeout");
  46. }catch(e){
  47. //console.log("timeout failed!");
  48. }
  49. }, timeout);
  50. return req;
  51. }

其原理就是利用nodejs请求的几个事件与计时器,一旦超出设定的响应时长则立马终结请求。如此,进度条一直卡着的问题解决了。
细心的读者可能看到了

  1. path.exists(filename, function(exists) {
  2. if (exists) {
  3. readFileEntry(filename,that.res);
  4. }else{...});

这段代码,其实这里做了一下服务端的图片缓存,一旦加载过的底图图片,直接从本地读取,极大的加快了地图的访问速度(这个在效率上提升了至少10倍)。
至于Arcgis API for Silverlight 是如何实现天地图底图以及其它底图服务(比如非标准墨卡托的地方坐标系底图服务)加载的呢?请听我下回分解。

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在vue-router中如何实现路由懒加载

在Angular2中如何实现断点调试ts文件

如何实现网页快报向上滚动

以上就是在nodejs中遇到404长时间未响应的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行