Javascript 当url不存在时打开(…)问题';t出口

Javascript 当url不存在时打开(…)问题';t出口,javascript,phantomjs,casperjs,Javascript,Phantomjs,Casperjs,我想让一个示例脚本允许检查url列表(从csv文件传递)。对于每个url,如果url是否返回错误(404500..),脚本将返回 因此,在几次尝试之后,我几乎做到了这一点,但是…我注意到当URL不退出时(例如;),我的casper.open在下一个casperjs步骤之前没有时间获取错误(500)。然后“开始”。我的意思是: 这是我的函数,它包含两个casperjs步骤 function check(line){ this.then(function(){ //step one ur

我想让一个示例脚本允许检查url列表(从csv文件传递)。对于每个url,如果url是否返回错误(404500..),脚本将返回

因此,在几次尝试之后,我几乎做到了这一点,但是…我注意到当URL不退出时(例如;),我的casper.open在下一个casperjs步骤之前没有时间获取错误(500)。然后“开始”。我的意思是:

这是我的函数,它包含两个casperjs步骤

function check(line){
  this.then(function(){ //step one
    url = line.split("|");
    this.echo("TEST0 : "+url[0]);
    this.open(url[0]);
  });

  this.then( function (){ //step two
    if (this.currentHTTPStatus === 404){
      this.warn("Error 404");
    }else if (this.currentHTTPStatus === 500){
      this.warn("Error 500");
    }else{
      this.echo("TEST1 : "+url[0]);
      this.echo("TEST2 : "+this.getCurrentUrl());
      this.echo("Link OK !");
    }
  });

}
通过几个this.echo(…),我的终端显示:

TEST0 : http://www.google.com 
TEST1 : http://www.google.com
TEST2 : http://www.google.com
LINK OK!
TEST0 : http://www.google.es
TEST1 : http://www.google.es
TEST2 : http://www.google.es
LINK OK!
TEST0 : http://www.google.azerty
TEST1 : http://www.google.azerty
TEST2 : http://www.google.es <----- getCurrentUrl() doesn't return http://www.google.azerty, has no time because this url doesn't exist?
LINK OK!
以及我剧本的完整代码

var casper = require("casper").create({
  pageSettings: {
    loadImages: false,
    loadPlugins: false
  }
});
var fs = require('fs');
var csv = casper.cli.get(0);
var url;

function check(line){
  this.then(function(){
    url = line.split("|");
    this.echo("TEST0 : "+url[0]);
    this.open(url[0]);
  });

  this.then( function (){
    if (this.currentHTTPStatus === 404){
      this.warn("Error 404");
    }else if (this.currentHTTPStatus === 500){
      this.warn("Error 500");
    }else{
      this.echo("TEST1 : "+url[0]);
      this.echo("TEST2 : "+this.getCurrentUrl());
      this.echo("Link OK !");
    }
  });

}

function main(){
  this.then(function(){
    var stream = fs.open(csv, 'r');
    var line = stream.readLine();
    line = stream.readLine();
    while(line){
      check.call(this, line);
      line = stream.readLine();
    }
  });
}

casper.start().then(main).run();

如果将
打开
交换为
然后打开
,其行为是否相同?注意:CasperJS运行在PhantomJS之上,PhantomJS根本不使用node。我尝试使用thenOpen,但没有找到如何传递从上一个.then生成的url参数(范围变量问题)。然而,我在代码的开头声明了url变量(那么,这个变量是全局的?)。因此,当我在第二步中使用“.thenOpen(url,函数…”时,程序返回一个错误(未找到变量)。我的意思是在不更改任何其他内容的情况下进行替换。这应该会导致您描述的错误。如果我用thenOpen替换open,问题仍然存在(我希望我理解您的意思x))
var casper = require("casper").create({
  pageSettings: {
    loadImages: false,
    loadPlugins: false
  }
});
var fs = require('fs');
var csv = casper.cli.get(0);
var url;

function check(line){
  this.then(function(){
    url = line.split("|");
    this.echo("TEST0 : "+url[0]);
    this.open(url[0]);
  });

  this.then( function (){
    if (this.currentHTTPStatus === 404){
      this.warn("Error 404");
    }else if (this.currentHTTPStatus === 500){
      this.warn("Error 500");
    }else{
      this.echo("TEST1 : "+url[0]);
      this.echo("TEST2 : "+this.getCurrentUrl());
      this.echo("Link OK !");
    }
  });

}

function main(){
  this.then(function(){
    var stream = fs.open(csv, 'r');
    var line = stream.readLine();
    line = stream.readLine();
    while(line){
      check.call(this, line);
      line = stream.readLine();
    }
  });
}

casper.start().then(main).run();