Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { fetch } from "https://esm.town/v/std/fetch";
export const isMyWebsiteDown = async () => {
const URL = "https://www.pcna.com/en-us/search/getsuggestions?query=0600-10";
const [date, time] = new Date().toISOString().split("T");
let ok = true;
let healthy = true;
let reason: string;
let responseData: string;
try {
const start = Date.now();
const res = await fetch(URL);
const millisecondsSpent = (Date.now() - start);
const jsonData = await res.json();
responseData = JSON.stringify(jsonData, null, "\t");
if (res.status !== 200) {
reason = `(status code: ${res.status})`;
ok = false;
}
if (millisecondsSpent > 1000) {
reason = `(Too slow. Returned in ${millisecondsSpent})`;
healthy = false;
}
}
catch (e) {
reason = `couldn't fetch: ${e}`;
ok = false;
}
if (!ok) {
const subject = `Website down (${URL})`;
const body =
`At ${date} ${time} (UTC), ${URL} was down (reason: ${reason}).`;
console.email(body, subject);
}
if (!healthy) {
const subject = `Website not healthy (${URL}) At ${date} ${time} (UTC)`;
const body =
`reason: ${reason}). \n\r The response was \n\r ${responseData}</pre>.`;
console.email(body, subject);
}
};
// Forked from @ktm.isMyWebsiteDown
October 23, 2023