如何从nodejs请求模块获取重定向的URL?

如何从nodejs请求模块获取重定向的URL?,第1张

如何从nodejs请求模块获取重定向的URL?

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

var r = request(url, function (e, response) {  r.uri  response.request.uri})

uri是一个对象。uri.href包含带有查询参数的URL作为字符串。

该代码来自请求创建者对github问题的评论:https
:
//github.com/mikeal/request/pull/220#issuecomment-5012579

例:

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);});

这将打印http://www.google.com/?q=foo
3次(请注意,我们从一个不带www的地址重定向到一个带有www的地址)。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4890379.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-11
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存