Last active
June 5, 2017 15:50
-
-
Save getdave/85b5001b4dea9378ab90389272e22e9f to your computer and use it in GitHub Desktop.
Basic 301 redirect checker using NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Crawler = require("crawler"); | |
var map = require("lodash.map"); | |
var chalk = require('chalk'); | |
var errorStyle = chalk.bold.red; | |
var successStyle = chalk.bold.green; | |
var errors = []; | |
var c = new Crawler({ | |
rateLimit: 200, | |
maxConnections: 3, | |
callback: function(error, res, done) { | |
var requestedUri = res.options.uri; | |
var targetUri = urlMap[requestedUri]; | |
var actualUri = res.request.uri.href; | |
console.log("Requesting: " + requestedUri); | |
if(error) { | |
console.log(error) | |
} else { | |
if (actualUri !== targetUri) { | |
console.log("Fail (" + actualUri + ") \n"); | |
errors.push(requestedUri + " ==> " + actualUri + "[" + res.statusCode + "] (expected: " + targetUri + ")"); | |
} else { | |
console.log("Success (" + actualUri + ") \n"); | |
} | |
} | |
done(); | |
} | |
}); | |
c.on('drain',function(){ | |
if (errors.length) { | |
errors.forEach(function(error){ | |
console.log( errorStyle( error ) + "\n"); | |
}); | |
} else { | |
console.log( successStyle( "All uris redirected correctly!") ); | |
} | |
}); | |
// Urls to test in format "old" => "new" | |
var urlMap = { | |
}; | |
var urls = map(urlMap, function(value, key) { | |
return key; | |
}); | |
c.queue(urls); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment