什么可能导致HTTP GET在代码中返回504而在404中通过curl返回?

时间:2017-01-15 16:58:51

标签: node.js curl httprequest

以下是代码:

  var request, tmUrl;

  request = require('request');

  tmUrl = "http://archive.is/timemap/https://www.washingtonpost.com/news/the-fix/wp/2017/01/15/rep-john-lewiss-books-sell-out-following-donald-trumps-attacks/";

  // this one works 
  request('http://www.google.com', function(error, response, body) {
    if (!error && response.statusCode === 200) {
      return console.log(body.slice(0, 301));
    }
  });

  // this one is always 504
  request(tmUrl, {
    timeout: 10000, // changing to 60s made no difference, 
                    // response comes back long before that 
    "User-Agent": 'curl/7.43.0'
  }, function(error, response, body) {
    if (!error) {
      if (response.statusCode === 200 || response.statusCode === 404) {
        return console.log(body.slice(0, 301));
      } else {
        console.error("response code ", response.statusCode);
        return console.log(body.slice(0, 301));
      }
    } else {
      return console.error(err);
    }
  });

问题是通过curl运行该请求会返回预期的响应,即带有此文本的404:TimeMap does not exists. The archive has no Mementos for the requested URI。但是当我在Node中运行它时,响应代码总是504,我不知道为什么。我最好的猜测是不允许用户代理,所以我让它们匹配。没工作。我不知道下一步该做什么......

504超时总是来自cloudflare:

<span class="cf-footer-item"><span data-translate="performance_security_by">Performance
&amp; security by</span> <a data-orig-proto="https" data-orig-ref="www.cloudflare.com/5xx-error-landing?utm_source=error_footer" id="brand_link" target="_blank">CloudFlare</a>
</span>

问题是,cloudflare阻止以某种我不知道的方式对网站进行编程访问吗?

1 个答案:

答案 0 :(得分:0)

无论如何,504请求不会返回结果,因为网关超时。 为什么不在代码中处理它? 例如。 if(response.statusCode === 200 || response.statusCode === 404 || response.statusCode === 504){