research/uptime-check-that-reads-the-page
A Small Uptime Check That Reads the Page: Timeouts, Redirects and "200 OK" Errors Tested
Engineering Note · November 18, 2026 · 9 minute read · Monitoring · Node.js · Testing · Reliability
An uptime check that only looks at the status code misses most of the ways a small site breaks, because a broken page usually still answers 200. We built a 30-line Node.js check that reads the whole body under one timeout, fails if the final URL is on another host, requires a known piece of text, rejects known error text, and sets a minimum size. Against a local server built to misbehave, the status-code check called four broken pages “up.” The 30-line check failed all eight broken cases and passed the working page.
Everything below was run with Node.js 22.23.5 (its built-in fetch is undici 6.29.1) on Linux. Every output shown is the real output from those runs. This is the check described in today’s article, Knowing Your Website Is Broken Before a Customer Tells You.
What did we test against?
A local server on port 8080 with one route per failure, plus a second server on 8081 standing in for a domain parking page. All page names and text are made up for the test.
// server.mjs
import http from 'node:http';
const page = (body) => `<!doctype html><html><head><title>Hypothetical Landscaping</title></head><body>${body}</body></html>`;
const good = page('<h1>Hypothetical Landscaping</h1><p>Call us for a quote.</p><form id="contact"></form>' + '<p>x</p>'.repeat(400));
const routes = {
'/ok': (req, res) => { res.writeHead(200, {'content-type':'text/html'}); res.end(good); },
'/error-page': (req, res) => { res.writeHead(200, {'content-type':'text/html'}); res.end(page('<h1>Error establishing a database connection</h1>')); },
'/blank': (req, res) => { res.writeHead(200, {'content-type':'text/html'}); res.end(''); },
'/server-error': (req, res) => { res.writeHead(500); res.end('Internal Server Error'); },
'/slow': (req, res) => { setTimeout(() => { res.writeHead(200, {'content-type':'text/html'}); res.end(good); }, 12000); },
'/trickle': (req, res) => { res.writeHead(200, {'content-type':'text/html'}); let i = 0; const t = setInterval(() => { res.write('<p>x</p>'); if (++i === 30) { clearInterval(t); res.end(good); } }, 500); },
'/loop': (req, res) => { res.writeHead(302, {location: '/loop2'}); res.end(); },
'/loop2': (req, res) => { res.writeHead(302, {location: '/loop'}); res.end(); },
'/moved': (req, res) => { res.writeHead(301, {location: 'http://127.0.0.1:8081/parked'}); res.end(); },
};
http.createServer((req, res) => (routes[req.url] || ((q, r) => { r.writeHead(404); r.end('Not found'); }))(req, res)).listen(8080, '127.0.0.1');
http.createServer((req, res) => { res.writeHead(200, {'content-type':'text/html'}); res.end(page('<h1>This domain may be for sale</h1>')); }).listen(8081, '127.0.0.1');
/trickle is the unusual one: it sends a 200 and its headers within about half a second, then drips the body out over 15 seconds.
What does a status-code check report?
The first version is the check most of us write first: fetch the URL, call it up if res.ok is true.
// naive.mjs
const base = 'http://127.0.0.1:8080';
for (const path of ['/ok', '/error-page', '/blank', '/server-error', '/loop', '/moved', '/slow']) {
const t0 = Date.now();
try {
const res = await fetch(base + path);
console.log(path.padEnd(14), res.ok ? 'UP ' : 'DOWN', res.status, `${Date.now() - t0} ms`);
} catch (e) {
console.log(path.padEnd(14), 'DOWN', e.cause?.message ?? e.message, `${Date.now() - t0} ms`);
}
}
$ node naive.mjs
/ok UP 200 56 ms
/error-page UP 200 4 ms
/blank UP 200 2 ms
/server-error DOWN 500 1 ms
/loop DOWN redirect count exceeded 14 ms
/moved UP 200 2 ms
/slow UP 200 12009 ms
Four broken pages reported as up. /moved is the one that surprises people: fetch follows the 301 to the parking page and reports that page’s 200. The redirect loop does fail, on its own. The Fetch standard stops at a redirect count of 20, and Node’s fetch reports redirect count exceeded as the error’s cause. /slow passes after 12 seconds because nothing told it to give up.
What does the 30-line check do differently?
Five things, one line or two each:
// check.mjs: is each page up, and is it the right page?
const TIMEOUT_MS = 8000;
const checks = JSON.parse(await import('node:fs').then((fs) => fs.readFileSync(process.argv[2], 'utf8')));
async function check({ url, mustContain, mustNotContain = [], minBytes = 1000 }) {
const t0 = Date.now();
try {
const res = await fetch(url, { signal: AbortSignal.timeout(TIMEOUT_MS), headers: { 'user-agent': 'uptime-check/1.0' } });
const body = await res.text(); // the timeout covers the body too
const ms = Date.now() - t0;
if (!res.ok) return [false, `HTTP ${res.status}`, ms];
if (new URL(res.url).host !== new URL(url).host) return [false, `redirected to ${res.url}`, ms];
if (body.length < minBytes) return [false, `only ${body.length} bytes`, ms];
if (!body.includes(mustContain)) return [false, `missing "${mustContain}"`, ms];
const bad = mustNotContain.find((s) => body.includes(s));
if (bad) return [false, `contains "${bad}"`, ms];
return [true, `HTTP ${res.status}`, ms];
} catch (e) {
const why = e.name === 'TimeoutError' ? `no answer in ${TIMEOUT_MS} ms` : (e.cause?.code ?? e.cause?.message ?? e.message);
return [false, why, Date.now() - t0];
}
}
const results = await Promise.all(checks.map(check));
let failed = 0;
results.forEach(([ok, why, ms], i) => {
if (!ok) failed++;
console.log(`${ok ? 'OK ' : 'FAIL'} ${checks[i].url.padEnd(34)} ${why} (${ms} ms)`);
});
process.exit(failed ? 1 : 0);
- One timeout around the whole request.
AbortSignal.timeout()stays attached whileres.text()reads the body. - Same host at the end.
res.urlis the final URL after redirects. A redirect to another host fails.hostincludes the port, sohttptohttpson the default ports still matches (we did not test HTTPS here). - A minimum size. It catches the empty
200. - Required text. Something that should always be on the working page. Here it is the form’s
id="contact". - Forbidden text. Phrases that mean a known failure, such as a database error or a parking page.
The checks run in parallel with Promise.all, so one slow page does not delay the rest, and the script exits 1 if anything failed. A scheduler or CI job can alert on that exit code.
The config file for the run:
[
{ "url": "http://127.0.0.1:8080/ok", "mustContain": "id=\"contact\"", "mustNotContain": ["database connection", "for sale"] },
{ "url": "http://127.0.0.1:8080/error-page", "mustContain": "id=\"contact\"", "mustNotContain": ["database connection", "for sale"] },
{ "url": "http://127.0.0.1:8080/blank", "mustContain": "id=\"contact\"" },
{ "url": "http://127.0.0.1:8080/server-error", "mustContain": "id=\"contact\"" },
{ "url": "http://127.0.0.1:8080/loop", "mustContain": "id=\"contact\"" },
{ "url": "http://127.0.0.1:8080/moved", "mustContain": "id=\"contact\"" },
{ "url": "http://127.0.0.1:8080/slow", "mustContain": "id=\"contact\"" },
{ "url": "http://127.0.0.1:8080/trickle", "mustContain": "id=\"contact\"" },
{ "url": "http://127.0.0.1:9/closed", "mustContain": "id=\"contact\"" }
]
$ node check.mjs checks.json; echo "exit $?"
OK http://127.0.0.1:8080/ok HTTP 200 (41 ms)
FAIL http://127.0.0.1:8080/error-page only 142 bytes (16 ms)
FAIL http://127.0.0.1:8080/blank only 0 bytes (16 ms)
FAIL http://127.0.0.1:8080/server-error HTTP 500 (16 ms)
FAIL http://127.0.0.1:8080/loop redirect count exceeded (29 ms)
FAIL http://127.0.0.1:8080/moved redirected to http://127.0.0.1:8081/parked (19 ms)
FAIL http://127.0.0.1:8080/slow no answer in 8000 ms (8001 ms)
FAIL http://127.0.0.1:8080/trickle no answer in 8000 ms (8002 ms)
FAIL http://127.0.0.1:9/closed bad port (1 ms)
exit 1
Every broken case fails, with a reason a person can act on. The whole run took about 8 seconds, the length of the timeout, because the checks run side by side.
Which check catches which failure?
The error page failed on size before it reached the text checks. To see each layer on its own, we ran the error page with the size floor off, once without forbidden text and once with it, and added a closed port and a name that cannot resolve:
$ node check.mjs checks2.json; echo "exit $?"
OK http://127.0.0.1:8080/error-page HTTP 200 (60 ms)
FAIL http://127.0.0.1:8080/error-page contains "database connection" (26 ms)
FAIL http://127.0.0.1:8099/ ECONNREFUSED (13 ms)
FAIL http://does-not-exist.invalid/ ENOTFOUND (13 ms)
exit 1
The first line is the warning. With a required string that also appears on the error page (<title> is on every page from this template), the check passes a broken page. Pick required text that only the working page has: the form, the phone number, a line from the footer. Do not pick the site name if your error template also shows it.
| Failure | Status code only | Host compare | Size floor | Required text | Forbidden text | Timeout |
|---|---|---|---|---|---|---|
| Error message in a 200 page | passes | passes | fails | fails | fails | passes |
| Empty 200 | passes | passes | fails | fails | passes | passes |
| Redirect to another host | passes | fails | depends | fails | fails if listed | passes |
| Redirect loop | fails | n/a | n/a | n/a | n/a | n/a |
| Slow headers (12 s) | passes | passes | passes | passes | passes | fails |
| Slow body (15 s) | passes | passes | passes | passes | passes | fails |
“Fails” is what we want in this table: it means the layer caught the problem. Required text catches the most on its own, which is why it matters so much what you choose.
Why must the timeout cover the body?
A common pattern sets a timer, aborts the fetch if it fires, and clears the timer when fetch() resolves. That stops the timer as soon as headers arrive. We ran that pattern against /trickle:
// headers-only.mjs: timeout wraps only the fetch() call, not the body read.
const t0 = Date.now();
const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), 8000);
const res = await fetch('http://127.0.0.1:8080/trickle', { signal: ctrl.signal });
clearTimeout(timer);
console.log(`headers after ${Date.now() - t0} ms, status ${res.status}`);
const body = await res.text();
console.log(`body after ${Date.now() - t0} ms, ${body.length} bytes`);
$ node headers-only.mjs
headers after 536 ms, status 200
body after 15100 ms, 3619 bytes
The headers came back in half a second with a 200, the timer was cleared, and the body took 15 seconds. A real slow server could hold that read open far longer. With AbortSignal.timeout() left in force while res.text() runs, the same route failed at 8,002 ms.
Why did port 9 say “bad port”?
We first used port 9 for the closed-port case, expecting ECONNREFUSED. Node’s fetch refused to connect at all and reported bad port. The Fetch standard keeps a list of “bad ports” that belong to other protocols and blocks requests to them; the list includes 9 (discard), 22 (SSH) and 25 (SMTP). Node’s fetch follows the list, so you cannot use fetch to check whether an SSH or SMTP port is open. Use net.connect for that. Port 8099, which nothing listens on, gave the ECONNREFUSED we expected.
What would we add for production?
- Fail twice before alerting. Run the check again a minute later and alert only if both runs fail, so one slow response does not page anyone.
- Run it from somewhere other than the site’s own host. A check on the same server goes down with it.
- Keep the config next to the site. When the contact form’s
idchanges, the check should change in the same commit.
Sources
- WHATWG, Fetch Standard, source file
fetch.bsread from github.com/whatwg/fetch (main branch) on November 17, 2026. Source for the bad-port list (ports 9, 22 and 25 among them) and the redirect limit of 20.
Limitations
- Every test ran against local servers over plain HTTP. We did not test HTTPS, certificate errors or HTTP/2 here.
res.text()holds the whole body in memory. That is fine for normal pages, but a check against a large file should read a limited number of bytes instead.- The check reads HTML as the server sends it. It does not run JavaScript, so a page whose content is drawn by script in the browser needs a headless browser check instead.
body.lengthcounts UTF-16 code units, not bytes. The label says bytes because the test pages are ASCII; treat the number as approximate on pages with other characters.- Timings are from one run on one machine and only show the order of magnitude.
Related reading
- Knowing Your Website Is Broken Before a Customer Tells You
- Heartbeat Checks for Scheduled Jobs: Alerting When a Job Did Not Run
- A Runbook for a One-Person Operation: What to Write Down First
Frequently asked questions
Why is checking for HTTP 200 not enough for uptime monitoring?
Many broken pages still return 200. In our test, a page showing a database error, an empty page, and a redirect to a parking page on another host all returned 200 after fetch followed the redirect. A check should also read the body and confirm that something which should always be there is present.
How do you set a timeout on fetch in Node.js?
Pass signal: AbortSignal.timeout(ms) in the fetch options and read the body while that signal is still in force. The signal then aborts a slow body as well as slow headers, and the error's name is TimeoutError. If you clear a manual timer as soon as fetch resolves, a server that sends headers quickly and the body slowly can keep the check waiting.
How do you detect that a page redirected to another site?
Let fetch follow redirects and compare new URL(res.url).host with the host you asked for. In our test a 301 to a parking page on another port returned a final status of 200, so only the host comparison caught it. A redirect loop needs no special code: Node's fetch fails with "redirect count exceeded".
Why does Node's fetch say "bad port"?
The Fetch standard blocks a list of ports used by other protocols, such as 9 (discard), 22 (SSH) and 25 (SMTP), and Node's fetch follows it. A request to one fails immediately with the cause "bad port" without connecting. Use a raw TCP connection, not fetch, to check a service on one of those ports.
What text should an uptime check look for?
Something that appears on the working page and would be missing from any failure, such as the contact form's id, the business phone number or a footer line. Add a short list of strings that should never appear, such as a known database error message or a parking page phrase, and a minimum body size to catch empty pages.