Make a GET request in Node.js
node-js
Updated August 29, 2026
Use the built-in fetch API for a simple GET request in a current Node.js release:
async function getData() {
const response = await fetch('https://example.com/data');
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
console.log(data);
}
getData().catch(console.error);
fetch() returns a promise; an HTTP 404 or 500 does not reject it by itself, which is why checking response.ok matters. Add a timeout with an AbortController for production code:
async function getWithTimeout(url) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 5000);
try {
const response = await fetch(url, { signal: controller.signal });
// validate the response before using it
return response;
} finally {
clearTimeout(timer);
}
}
getWithTimeout('https://example.com/data').catch(console.error);
Validate untrusted JSON and avoid logging credentials or personal data. For older Node versions that lack the global fetch implementation, use the version's documented HTTP client or upgrade according to the project requirements.
Sources
related.
Ruslan Osipov
About the author