Globals

Response.ok

Indicates an HTTP success status from 200 through 299. Fetch generally fulfills for an HTTP 404 or 503, so check the response status before treating the operation as successful. Transport errors and aborts use promise rejection. Tested on Node.js 22.16.0. Example output (one line per entry): ["false 404 missing"].

Formal Syntax & Structure

nodejs syntaxnodejs
response.ok

Return Value

boolean

Whether the response has a 2xx status.

Working Example

Example Snippetnodejs
import http from 'node:http';const server = http.createServer((q, r) => { r.statusCode = 404; r.end('missing'); });await new Promise(r => server.listen(0, '127.0.0.1', r));const url = 'http://127.0.0.1:' + server.address().port;try {    const r = await fetch(url);    console.log(r.ok, r.status, await r.text());}finally {    server.closeAllConnections();    await new Promise(r => server.close(r));}
Node.js Reference