【问题标题】:Node.js Requests returning 301 redirectsNode.js 请求返回 301 重定向
【发布时间】:2012-06-19 03:21:50
【问题描述】:

我是 node.js 的新手,但我想尝试一些基本代码并提出一些请求。目前,我正在使用 OCW 搜索 (http://www.ocwsearch.com/),并尝试使用他们的示例搜索请求提出一些基本请求:

但是,无论我尝试提出什么请求(即使我只是查询 google.com),它都会返回给我

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/0.7.65</center>
</body>
</html>

我不太清楚发生了什么。我查找了 nginx,但大多数关于它的问题似乎都是由设置自己的服务器的人提出的。我尝试使用 https 请求,但返回错误“ENOTFOUND”。

我的代码如下:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');

    var options = {
      host:'ocwsearch.com',
      path:
      '/api/v1/search.json?q=statistics&contact=http%3a%2f%2fwww.ocwsearch.com%2fabout/',
      method: 'GET'
    }  


    var req = http.request(options, function(res) {
      console.log("statusCode: ", res.statusCode);
      console.log("headers: ", res.headers);
      res.on('data', function(d) {
            process.stdout.write(d);
      });
    });
    req.end();

    req.on('error', function(e) {
      console.error(e);
    });


}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

抱歉,这是一个非常简单的问题,感谢您提供的任何帮助!

【问题讨论】:

  • 这是一大笔钱。

标签: javascript node.js http nginx http-redirect


【解决方案1】:

对我来说,我试图 GET 的网站将我重定向到安全协议。所以我改变了

require('http');

require('https');

【讨论】:

    【解决方案2】:

    问题是 Node.JS 的 HTTP 请求模块没有遵循您给出的重定向。

    请参阅此问题了解更多信息:How do you follow an HTTP Redirect in Node.js?

    基本上,您既可以查看标头并自己处理重定向,也可以为此使用少数几个模块之一。我已经使用了“请求”库,并且我自己也很幸运。 https://github.com/mikeal/request

    【讨论】:

    • 非常感谢您的快速回复!我去看看。
    • 上面的$是怎么回事,是故意的吗?
    • @jcolebrand,它们看起来像是他从编辑器那里复制和粘贴的。它们似乎在指示行尾字符。
    • 是的,我已经看过几次了,我将不得不检查 node.js 聊天,看看他们是否知道是什么原因造成的。
    【解决方案3】:
    var http = require('http');
    
    var find_link = function(link, callback){
    
      var root =''; 
    
      var f = function(link){
    
        http.get(link, function(res) {
    
          if (res.statusCode == 301) {
            f(res.headers.location);
          } else {
            callback(link);
          } 
    
        });
     }
    
      f(link, function(t){i(t,'*')});
    }   
    
    find_link('http://somelink.com/mJLsASAK',function(link){
      console.log(link);
    });
    
    function i(data){
      console.log( require('util').inspect(data,{depth:null,colors:true}) )
    }
    

    【讨论】:

    • 那么为什么res.statusCode == 301 在这一点上,i 在哪里以及您使用什么混淆器来生成此代码:P
    • 哦,f 是递归的; àla line ~12...LOL
    • 添加了 i 函数,这肯定不会让它变得不那么混乱....至于混淆器哇,我不记得写这个我一定是被附身了。
    【解决方案4】:

    这个问题现在很老了,但我遇到了同样的 301 错误,这些答案实际上并没有帮助我解决问题。

    我写了同样的代码:

    var options = {
        hostname: 'google.com',
        port: 80,
        path: '/',
        method: 'GET',
        headers: {
           'Content-Type': 'text/plain',
        }
    };
    
    var http = require('http');
    
    var req = http.request(options, function(res) {
        console.log('STATUS:',res.statusCode);
        console.log('HEADERS: ', JSON.stringify(res.headers));
        res.setEncoding('utf8');
    
        res.on('data', function(chunk) {
            console.log(chunk);
        });
        res.on('end', function() {
            console.log('No more data in response.');
        });
    });
    
    req.on('error', function(e) {
        console.log('problem with request: ', e.message);
    });
    console.log(req);
    
    req.end();
    

    所以过了一段时间我意识到这段代码中有一个非常小的错误,它是主机名部分:

     var options = {
         hostname: 'google.com',
         ...
    

    您必须添加“www”。在你的 URL 之前获取 html 内容,否则会出现 301 错误。

    var options = {
         hostname: 'www.google.com',
    

    【讨论】:

    • 301 响应不是错误,它是永久重定向,也是此人问题的核心基础。使用 http 301 响应重定向的站点和 node.js http 不遵循重定向,您必须为此编写代码或使用可以为您执行此操作的众多模块之一。
    • 谢谢你,哈哈,几个小时,我没有注意到 www 不见了
    猜你喜欢
    • 2012-02-28
    • 2013-12-19
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2021-01-05
    • 2012-02-27
    • 1970-01-01
    相关资源
    最近更新 更多