Node.js 如何从nodejs请求模块获取重定向的url?

Node.js 如何从nodejs请求模块获取重定向的url?,node.js,module,request,Node.js,Module,Request,我正试图通过一个url来使用nodejs将我重定向到另一个页面 通过梳理文档,我找不到任何允许我在重定向后检索url的内容 我的代码如下: var request = require("request"), options = { uri: 'http://www.someredirect.com/somepage.asp', timeout: 2000, followAllRedirects: true }; request( option

我正试图通过一个url来使用nodejs将我重定向到另一个页面

通过梳理文档,我找不到任何允许我在重定向后检索url的内容

我的代码如下:

var request = require("request"),
    options = {
      uri: 'http://www.someredirect.com/somepage.asp',
      timeout: 2000,
      followAllRedirects: true
    };

request( options, function(error, response, body) {

    console.log( response );

});

request
默认获得重定向,默认可以通过10个重定向。你可以在网上查一下。这样做的缺点是,默认情况下,您不知道您得到的url是重定向的还是原始的

例如:

request('http://www.google.com', function (error, response, body) {
    console.log(response.headers) 
    console.log(body) // Print the google web page.
})
输出

> { date: 'Wed, 22 May 2013 15:11:58 GMT',
  expires: '-1',
  'cache-control': 'private, max-age=0',
  'content-type': 'text/html; charset=ISO-8859-1',
  server: 'gws',
  'x-xss-protection': '1; mode=block',
  'x-frame-options': 'SAMEORIGIN',
  'transfer-encoding': 'chunked' }
但如果您将选项
followRedirect
设置为false

request({url:'http://www.google.com',followRedirect :false}, function (error, response, body) {
    console.log(response.headers) 
    console.log(body)
});
它给

> { location: 'http://www.google.co.in/',
  'cache-control': 'private',
  'content-type': 'text/html; charset=UTF-8',
  date: 'Wed, 22 May 2013 15:12:27 GMT',
  server: 'gws',
  'content-length': '221',
  'x-xss-protection': '1; mode=block',
  'x-frame-options': 'SAMEORIGIN' }
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.in/">here</A>.
</BODY></HTML>
{位置:'http://www.google.co.in/',
“缓存控制”:“专用”,
“内容类型”:“text/html;charset=UTF-8”,
日期:2013年5月22日星期三格林威治标准时间15:12:27,
服务器:“gws”,
“内容长度”:“221”,
“x-xss-protection”:“1;模式=块”,
“x-frame-options”:“SAMEORIGIN”}
302移动
302移动
文档已移动
.

因此,不要担心获取重定向内容。但是,如果您想知道它是否被重定向,请设置
followRedirect
false并检查响应中的
位置
标题。

有两种非常简单的方法来获取重定向链中的最后一个url

var r = request(url, function (e, response) {
  r.uri
  response.request.uri
})
uri是一个对象。uri.href以字符串形式包含带有查询参数的url

代码来自请求创建者对github问题的评论:

例如:

var request = require('request');
var r = request.get('http://google.com?q=foo', function (err, res, body) {
  console.log(r.uri.href);
  console.log(res.request.uri.href);

  // Mikael doesn't mention getting the uri using 'this' so maybe it's best to avoid it
  // please add a comment if you know why this might be bad
  console.log(this.uri.href);
});

这将打印三次(请注意,我们被重定向到一个有www的地址,从一个没有www的地址)。

要查找重定向url,请尝试以下操作:

var url = 'http://www.google.com';
request({ url: url, followRedirect: false }, function (err, res, body) {
  console.log(res.headers.location);
});

您可以使用
followRedirect
(而不是
followAllRedirects
)的函数形式,如下所示:

options.followRedirect = function(response) {
  var url = require('url');
  var from = response.request.href;
  var to = url.resolve(response.headers.location, response.request.href);
  return true;
};

request(options, function(error, response, body) {
  // normal code
});

我不明白你的最后一句话“如果你想知道它是否被重定向,请设置
followRedirect
false
”?这不会停止重定向过程吗?默认情况下,由于自动重定向,您不会得到3xx响应。所以,如果你想知道你被重定向了/不想重定向,你必须把它设为false。这只是为了找出重定向。问问你自己,你是否被重定向了(就像在《盗梦空间》中)?你需要知道你得到的是重定向(又称梦境)或直接页面(或现实)。这是你的图腾;)你说上面的代码重定向了三次,我怎么知道哪一次运行是最后一次迭代?它没有重定向三次。它以三种不同的方式打印重定向到的URL。如果不清楚,很抱歉。@Gabrief,没有
这个
,因为我们可能会使用
es6
res.request.uri.href
如果给定的url是错误的url,例如'sdfdsfdgdfgdfgg.sdfsdfdf',就会崩溃,因此如果您想使用此选项,您可以检查
错误或
res
的存在。在查找“不要在节点中的请求中跟踪重定向”非常感谢!我不确定如何包括该选项。
res.headers.location
为我完成了这项工作。如果涉及多个重定向,可能无法按预期工作,给出第一个重定向,而不是最后一个重定向。