Is it possible to ping a server from Javascript?

后端 未结 17 902
礼貌的吻别
礼貌的吻别 2020-11-22 04:02

I\'m making a web app that requires that I check to see if remote servers are online or not. When I run it from the command line, my page load goes up to a full 60s (for 8 e

17条回答
  •  一向
    一向 (楼主)
    2020-11-22 04:56

    let webSite = 'https://google.com/' 
    https.get(webSite, function (res) {
        // If you get here, you have a response.
        // If you want, you can check the status code here to verify that it's `200` or some other `2xx`.
        console.log(webSite + ' ' + res.statusCode)
    }).on('error', function(e) {
        // Here, an error occurred.  Check `e` for the error.
        console.log(e.code)
    });;
    

    if you run this with node it would console log 200 as long as google is not down.

提交回复
热议问题