使用NodeJS收集所有redirect的URL的URL
我想检索从源URL Xredirect的URL列表,它可能有许多redirect的URL,但我想要它的所有列表。
例如:
http://www.example.com/origin-url
它将redirect到
http://www.example.com/first-redirect
再次将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
给我这个build议,我应该使用哪个节点模块来实现这一点。
提前致谢。
你可以使用NodeJS的http
模块。 你将需要检查statusCode
,redirect在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('/');