Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用NodeJS收集源URL的所有重定向URL_Node.js_.htaccess_Express_Redirect - Fatal编程技术网

Node.js 使用NodeJS收集源URL的所有重定向URL

Node.js 使用NodeJS收集源URL的所有重定向URL,node.js,.htaccess,express,redirect,Node.js,.htaccess,Express,Redirect,我想检索一个从源URL X重定向的URL列表,它可能有许多重定向的URL,但我想要它的所有列表 例如: http://www.example.com/origin-url 它将重定向到 http://www.example.com/first-redirect 它将再次重定向到 http://www.example.com/second-redicect 最后是这个 http://www.example.com/final-url 所以我想要的是使用NodeJs或Express的所有这些

我想检索一个从源URL X重定向的URL列表,它可能有许多重定向的URL,但我想要它的所有列表

例如:

http://www.example.com/origin-url
它将重定向到

http://www.example.com/first-redirect
它将再次重定向到

http://www.example.com/second-redicect
最后是这个

http://www.example.com/final-url
所以我想要的是使用NodeJs或Express的所有这些URL的列表

 http://www.example.com/origin-url -->> http://www.example.com/first-redirect
 -->> http://www.example.com/second-redicect -->> http://www.example.com/final-url
给我一些建议,我应该使用哪个节点模块来实现这一点


提前感谢。

您可以使用NodeJS的
http
模块。您需要检查
状态代码
,重定向的状态代码在300-400之间。请看下面的代码

    var http = require('http')


    function listAllRedirectURL(path) {
      var reqPath = path;
      return new Promise((resolve, reject) => {
        var redirectArr = [];

        function get(reqPath, cb){
            http.get({
              hostname: 'localhost',
              port: 3000,
              path: reqPath,
              agent: false  // create a new agent just for this one request
            }, (res) => {
              cb(res)
            }); 
        }

        function callback(res) {
            if (res.headers.hasOwnProperty('location') && res.statusCode >= 300 && res.statusCode < 400) {
               console.log(res.headers.location);
               redirectArr.push(res.headers.location);
               reqPath = (res.headers.location);
               get(reqPath, callback);
            } else {
              resolve(redirectArr);
            }
        }

        get(reqPath, callback);
      })
    }

    listAllRedirectURL('/');
var http=require('http'))
函数listAllRedirectURL(路径){
var reqPath=路径;
返回新承诺((解决、拒绝)=>{
var redirectArr=[];
函数get(请求路径,cb){
http.get({
主机名:“localhost”,
港口:3000,
路径:reqPath,
代理:false//仅为此请求创建一个新代理
},(res)=>{
cb(res)
}); 
}
函数回调(res){
if(res.headers.hasOwnProperty('location')&&res.statusCode>=300&&res.statusCode<400){
日志(res.headers.location);
重定向arr.push(res.headers.location);
reqPath=(res.headers.location);
get(请求路径,回调);
}否则{
解决(重定向);
}
}
get(请求路径,回调);
})
}
listAllRedirectURL('/');

但是在这里,我不知道url的主机名和端口,我将从以前的重定向中获得这些url,因此,我给出了一个示例,其中重定向是在同一主机的不同路径上完成的。因此,您可以将主机附加到
重定向arr
的每个元素。如果我想使用VPN重定向特定国家的url,该怎么办??有什么想法吗??