Node.js GET Requests with request, fetch, http, and Express

GET requests are HTTP requests used to read a specified resource or data from a server. In Node.js, the phrase handle GET requests can mean two different tasks: making an outbound GET request from your Node.js program to another website, or handling an inbound GET request sent by a browser or API client to your own server.

In this Node.js tutorial, we first keep the original example that makes outbound GET requests using the request module. Then we add current alternatives using the built-in fetch() API and the core http/https modules. For inbound GET routes, Express uses app.get() to handle a GET request for a path.

To learn about creating HTTP Web Server, please refer Create a HTTP Web Server in Node.js.

Outbound GET request vs Express GET route in Node.js

Node.js GET request taskTypical toolWhat it does
Call another website or APIfetch(), https, request, axios, gotYour Node.js program acts as an HTTP client and reads data from another server.
Respond to a browser or API clientNode http module or Express app.get()Your Node.js program acts as an HTTP server and sends a response to the requester.
Read query string dataURL, req.query in ExpressYour server reads values from a URL such as /search?q=node.

The original request package was widely used for outbound HTTP calls. The package is now deprecated, so new Node.js projects should normally use fetch(), the built-in http/https modules, or a maintained HTTP client library. This page still explains request because existing projects may contain it and the original tutorial is based on it.

Handle outbound GET requests using request Node.js module

There is module called ‘request’ for Node.js, which could help us to make get requests to another website. We shall start with installing the request Node.js module.

Install request Node.js module for legacy code

Open a Terminal or Command prompt and run the following command to install request Node.js module.

$ npm install request

For new code, prefer a maintained approach instead of adding this dependency. If you are maintaining an older application, keep the dependency pinned and test error handling before upgrading Node.js or deployment environments.

Example 1 – Node.js file with request module GET request

Following is an example Node.js file in which we shall include request module. And make a get request for the resource “http://www.google.com”. Call back function provided as second argument receives error(if any), response and body.

serverGetRequests.js

</>
Copy
// Example to Handle Get Requests using request Node.js module
// include request module
var request = require("request");

// make a get request for the resource "http://www.google.com"
request("http://www.google.com",function(error,response,body)
{
	console.log(response);
});

Open a terminal and run the following command to execute this Node.js file.

$ node serverGetRequests.js

The response would be echoed back to the console.

If there is no error with the get request, the content of error would be null. This information could be used as a check if there is any error in the get request made for a resource.

In practical programs, print only the fields you need instead of logging the full response object. For example, the response status code, response headers, and response body are usually easier to inspect than the full object.

</>
Copy
request("https://example.com", function (error, response, body) {
  if (error) {
    console.error("Request failed:", error.message);
    return;
  }

  console.log("Status:", response.statusCode);
  console.log("Content type:", response.headers["content-type"]);
  console.log("Body:", body);
});

Example 2 – Node.js request module GET request receiving error

There could be scenarios during which we might get an error for Get Request we do for a resource. Following example is a scenario of such where the url provided is bad.

serverGetRequestsError.js

</>
Copy
// include request module
var request = require("request");

// make a get request for the resource "http://www.go1411ogle.com"
request("http://www.go1411ogle.com",function(error,response,body)
{
	console.log(error);
});

Terminal Output

$ node serverGetRequestsError.js 
{ Error: getaddrinfo ENOTFOUND www.go1411ogle.com www.go1411ogle.com:80
    at errnoException (dns.js:53:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:95:26)
  code: 'ENOTFOUND',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'www.go1411ogle.com',
  host: 'www.go1411ogle.com',
  port: 80 }

The ENOTFOUND error normally means the host name could not be resolved by DNS. Similar network errors may occur when the remote server is down, the URL is wrong, the network is unavailable, or a proxy/firewall blocks the request.

Modern Node.js GET request using built-in fetch()

In modern Node.js versions, fetch() is available as a built-in API for making HTTP requests. It is a good first choice for simple GET calls because it does not require installing the deprecated request package.

getWithFetch.js

</>
Copy
async function getPage() {
  try {
    const response = await fetch("https://example.com");

    if (!response.ok) {
      throw new Error(`HTTP error: ${response.status}`);
    }

    const body = await response.text();
    console.log(body);
  } catch (error) {
    console.error("GET request failed:", error.message);
  }
}

getPage();

Run the file from the terminal.

</>
Copy
node getWithFetch.js

If the remote API returns JSON, use await response.json() instead of await response.text().

getJsonWithFetch.js

</>
Copy
async function getJsonData() {
  try {
    const response = await fetch("https://jsonplaceholder.typicode.com/posts/1");

    if (!response.ok) {
      throw new Error(`HTTP error: ${response.status}`);
    }

    const data = await response.json();
    console.log(data.title);
  } catch (error) {
    console.error("Unable to read JSON:", error.message);
  }
}

getJsonData();

Node.js GET request using the built-in https module

The core https module can make GET requests without any external package. This approach is useful when you want no extra dependency, but it requires more code than fetch().

getWithHttps.js

</>
Copy
const https = require("https");

https.get("https://example.com", (response) => {
  let body = "";

  response.on("data", (chunk) => {
    body += chunk;
  });

  response.on("end", () => {
    console.log("Status:", response.statusCode);
    console.log(body);
  });
}).on("error", (error) => {
  console.error("GET request failed:", error.message);
});

Use http for plain HTTP URLs and https for HTTPS URLs. For most application code, fetch() is easier to read, while the core modules give lower-level control.

Handle inbound GET requests in Express using app.get()

When the requirement is to handle GET requests sent to your own Node.js server, use a server framework such as Express. In Express, app.get() registers a route handler for the GET method and a specific path.

expressGetRoute.js

</>
Copy
const express = require("express");

const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Home page from a GET request");
});

app.get("/search", (req, res) => {
  const term = req.query.q || "";
  res.json({
    query: term,
    message: `You searched for ${term}`
  });
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

Install Express and run the file.

</>
Copy
npm install express
node expressGetRoute.js

Open the following URLs in a browser or API client.

</>
Copy
http://localhost:3000/
http://localhost:3000/search?q=nodejs

The first route sends plain text. The second route reads the query string value from req.query.q and returns JSON.

Common Node.js GET request error checks

CheckWhy it matters
Validate the URLA misspelled host name can produce DNS errors such as ENOTFOUND.
Check the status codeA successful network call can still return 404, 401, 403, or 500.
Read response body typeUse text() for HTML/text and json() for JSON when using fetch().
Handle timeout or slow remote APIsExternal calls can make your server response slow if not controlled.
Do not log secretsHeaders and URLs may contain tokens or private query parameters.

QA checklist for editing a Node.js GET request tutorial

  • Confirm whether the tutorial is explaining outbound GET calls or inbound Express GET routes.
  • Keep legacy request module examples clearly marked if the post title is about the request package.
  • Add a modern fetch() example for new Node.js projects.
  • Show error handling for DNS errors and non-2xx HTTP status codes.
  • Use HTTPS URLs in new examples unless the topic specifically requires plain HTTP.
  • Test every command and file name in the tutorial flow.
  • Use PrismJS language classes for any newly added code blocks.

Official references for Node.js fetch and Express GET routing

Node.js GET request FAQs

Which module in Node.js is used to handle HTTP requests and responses?

The built-in http module can create HTTP servers and handle requests and responses. For HTTPS URLs, Node.js also provides the built-in https module. For simpler application routing, many projects use Express on top of Node.js.

Can I use fetch in Node.js for GET requests?

Yes. Modern Node.js versions provide a built-in fetch() API for making HTTP requests. It is usually a better choice for new code than the deprecated request package.

Which Express method is used to handle GET requests?

Express uses app.get(path, handler) to handle GET requests for a specific route. For example, app.get(“/users”, handler) handles GET requests sent to the /users path.

Does the request module still work in Node.js?

The request package may still work in older projects, but it is deprecated and should not be the default choice for new projects. Use fetch(), the built-in http/https modules, or a maintained HTTP client library for new code.

How does Node.js handle multiple GET requests?

Node.js uses an event-driven, non-blocking I/O model. This allows it to keep accepting work while network and file operations complete asynchronously. Your route handlers and outbound HTTP calls should avoid blocking code so the server can continue handling other requests.

Summary: choose the right Node.js GET request method

In this Node.js Tutorial, we have learnt how to handle Get Requests to other websites from our HTTP Web Server in Node.js using request module. For new Node.js code, prefer fetch() or the built-in http/https modules for outbound GET requests. For GET requests coming into your own web application, use an HTTP server or Express app.get() route handlers.