今天我无心中看到一个库:website-failure-check-box,做用应该是检测网站的可用性,以为挺有意思的,因而看了下它的源码,记录下来,供之后开发时参考,但愿对其它人也有用。ios
检测网站可用性的核心代码以下:git
let exist = false for (let i = 0; i < 3; i++) { // tip: check 3 times exist |= await axios.get(site).then(response => response.status !== 404).catch(error => signale.error(error)) }
能够看到,它是经过使用 axios 的 get 方法,而后判断返回的 status 是否是 404 来进行检测的。为了不偏差,检测了三次,而且用|=
运算符进行汇总。github
这个库是利用 github actions 进行定时自动检测的,github actions 我就不在这里介绍了,请自行了解。web