aendrew aendrew - 1 year ago 126
Javascript Question

Detecting a 301 redirect or 404 in JSDom

I'm using JSDom to scrape pages and check page existence on bunch of S3 buckets. My buckets redirect to an page on 404 that then redirects to my site's main page. While trying to load a totally non-existent domain yields a

error, the
object is empty if I get a 404.

Using JSDom, how do I detect a redirect or a 404?

Answer Source

You could use request and get the statusCode. them pass the HTML fragment to JSDOM like so:

var jsdom = require("jsdom");
var request = require('request'); 
request(url, function (err, response, body) {
    if (!err && response.statusCode == 200) {
            scripts: [""],
            done: JS_DOM_FUNCTION()
    //You can handle any other status here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download