简体   繁体   English

Node.js https 请求卡住了

[英]Node.js https request stuck

I am a node.js beginner and I'm trying to check status of domains I read from a csv file.我是一个 node.js 初学者,我正在尝试检查我从 csv 文件中读取的域的状态。 All works fine, however when the script reaches the end it simply hangs and does not quit.一切正常,但是当脚本到达末尾时,它只是挂起并且不会退出。 Below is the code I'm working with.下面是我正在使用的代码。 What am i missing here?我在这里缺少什么? Any help would be greatly appreciated!任何帮助将不胜感激!

```
const fs = require('fs');
const request = require('request');
let https = require('https');

const input_path = 'Path_to_csv'



function FetchUrls (callback) {

     fs.readFile(input_path, 'utf8', function (err, data) {

     let dataArray = data.split(/\r?\n/);

     console.log(`loaded ${dataArray.length} items`)

     callback(dataArray)});

     }


function getData (dataArray) {

     let urls = dataArray

     for (let url of urls) {

         https.get(url, function(res) {

         if(res.statusCode == 500) {

            console.log("Domain Down")

         } else {

            console.log("Domain Up")
         }

         }).on('error', function(e) {

             console.log(e)
             process.exit()

    });

   }
}


FetchUrls(function(dataArray) {
getData(dataArray)

})
```

You need to add a return after the for loop.您需要在 for 循环后添加一个 return 。 But I would wrap it in a promise like:但我会把它包装在一个承诺中:

FetchUrls(input_path, function(dataArray) {
  let promises = []
  for (let domain of dataArray) {
    promises.push(new Promise((resolve, reject) => {
      // ... do the https.getting things, and then resolve() where you think it should end
    }))
  }
   Promise.all(promises).then( Nothing => return Nothing)
   .catch(error => return error)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM