我想回忆一下readline。怎么做?

时间:2017-09-19 13:29:33

标签: javascript node.js

我想回忆一下

rl.question("input your port do you want (about 0 - 65535) : ", function(portnumber)

之后:

if(netinfo > 6665){
    return console.log("Error stupid i say about 0 - 65535")
}

rl.question("input your port do you want (about 0 - 65535) : ", function(portnumber){
   if(netinfo > 6665){
     return console.log("Error stupid i say about 0 - 65535")
    } 

     netinfo = portnumber;

     const requestHandler = (request, response) => {  
      console.log(request.url)
      response.write('<b>welcome to groone simple http server :p </b>');
      response.end('ahahahahahah :)');
    }

怎么做?

2 个答案:

答案 0 :(得分:1)

我看到的唯一方法是递归:

(function restart(){
  rl.question("input your port do you want (about 0 - 65535) : ", function(portnumber){
    if(portnumber > 6665){
       console.log("Error stupid i say about 0 - 65535");
       return restart();
    }
    //...
  });
})()

答案 1 :(得分:0)

当我们收到正确/预期的答案时,你需要一个无限循环。

示例代码:

var correctAnswer = false;
while(!correctAnswer){
    doStuff()
    if(0 < receivedAnswer && receivedAnswer  < 6665) correctAnswer = true;
}