How GET Requests Work in Node.js APIs

Learn how GET requests work in Node.js using the built-in HTTP module. Understand routing, query parameters, status codes, and real-world API examples.
GET Requests in Node.js: Fetching Data from APIs and Servers
After understanding CRUD operations, it's time to explore the first and most commonly used HTTP method in REST APIs: the GET request .
Whenever you open a website, load products in an e-commerce app, view blog posts, or fetch user profiles, a GET request is usually working behind the scenes.
In this article, you'll learn how GET requests work in Node.js, when to use them, and how they fit into REST API development.
What is a GET Request?
A GET request is used to retrieve data from a server .
Unlike POST or PUT requests, GET requests should never modify data on the server. Their only purpose is to fetch information.
For example:
- Fetch all users
- Retrieve a single blog post
- Load product details
- Display comments
- Get analytics data
In the previous article on CRUD Operations in Node.js , we learned that the Read operation maps directly to the GET HTTP method.
Basic GET Request Structure
A simple GET request looks like this:
GET /users
This tells the server:
"Send me the list of users."
If we want a specific user:
GET /users/1
This means:
"Send me the user with ID 1."
Creating a GET Route in Node.js
Using the built-in http module:
const http = require("http");
const users = [
{ id: 1, name: "John" },
{ id: 2, name: "Alice" }
];
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/users") {
res.writeHead(200, {
"Content-Type": "application/json"
});
res.end(JSON.stringify(users));
}
});
server.listen(3000, () => {
console.log("Server running on port 3000");
});
If the client visits:
http://localhost:3000/users
The server responds with:
[
{
"id": 1,
"name": "John"
},
{
"id": 2,
"name": "Alice"
}
]
Understanding req.method
Every incoming request contains an HTTP method.
Node.js provides it through:
req.method
Example:
console.log(req.method);
Output:
GET
Later, when we build POST, PATCH, and DELETE endpoints, we'll use this property to determine which action should run for each request.
Understanding req.url
The request URL tells us which resource the client wants.
Example:
console.log(req.url);
If the user visits:
/users
The output becomes:
/users
If the user visits:
/users/1
The output becomes:
/users/1
Combining req.method and req.url allows us to create simple routing logic without external frameworks.
Returning a Single Resource
Fetching all users is common, but APIs often need to return a single item.
Example:
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/users/1") {
const user = {
id: 1,
name: "John"
};
res.writeHead(200, {
"Content-Type": "application/json"
});
res.end(JSON.stringify(user));
}
});
Response:
{
"id": 1,
"name": "John"
}
GET Requests Should Never Modify Data
A common beginner mistake is using GET requests to delete or update resources.
Wrong:
GET /delete-user/1
Correct:
DELETE /users/1
Similarly:
GET /update-user/1
Should instead be:
PATCH /users/1
Each HTTP method has a specific responsibility, which keeps APIs predictable and easier to maintain.
Sending Query Parameters
GET requests often include additional information using query parameters.
Example:
/products?category=laptop
Or:
/users?page=2
These parameters help the server filter, sort, and paginate data.
Examples include:
- Product filters
- Search functionality
- Pagination
- Sorting
- Date ranges
We'll explore URL parsing and query parameters in greater detail when working with the URL module and advanced API routing.
Common GET Request Examples
Fetch All Products
GET /products
Fetch a Single Product
GET /products/5
Search Products
GET /products?search=keyboard
Paginate Results
GET /products?page=2&limit=10
These patterns are used in nearly every production API.
Common Status Codes for GET Requests
| Status Code | Meaning |
|---|---|
| 200 | Request successful |
| 404 | Resource not found |
| 400 | Invalid request |
| 500 | Internal server error |
Understanding status codes makes debugging APIs significantly easier. We'll cover them in detail in our dedicated guide on HTTP Status Codes in Node.js .
Testing GET Requests with Postman
You can test GET requests using:
- Postman
- Thunder Client
- Insomnia
- Browser address bar
- Frontend applications
For Postman:
- Select
GET - Enter the endpoint URL
- Click Send
- View the response
We'll build a complete API testing workflow later in our API Testing with Postman article.
Real World Examples of GET Requests
GET requests power almost every modern application:
- Loading social media feeds
- Displaying products
- Fetching notifications
- Loading dashboards
- Showing blog posts
- Retrieving user profiles
Without GET requests, websites would have no data to display.
Conclusion
GET requests are the foundation of data retrieval in Node.js applications.
They are fast, simple, cache-friendly, and power nearly every interaction users have with modern applications.
Understanding how GET requests work at the HTTP level will make it much easier to build REST APIs, work with frameworks like Express, and design production-ready backend systems.
Master GET requests first, because every backend developer uses them every single day.