Working with Network and HTTP API in JavaScript (Node.js): Detailed Guide
What is it and why? (a real-life analogy)
Imagine you are sitting in a cafe and want to order a pizza. You don't go to the kitchen and cook it yourself — you simply call the waiter, tell them your order, and after a while, they bring you the finished pizza. This simple action hides a whole philosophy of interaction between programs.
Working with the network and HTTP API is exactly the same thing, only instead of a waiter we have the HTTP protocol, instead of a kitchen — a remote server, and instead of pizza — data (for example, a list of movies, a weather forecast, or user information).
In real life, we constantly use such "waiters": when you open a weather app, it sends a request to the server and receives data. When you search for a product on a marketplace — the browser asks the server to show results. Even when you are reading this article, your browser has already made dozens of HTTP requests to load text, images, and fonts.
Why does a programmer need this? Because modern applications rarely work alone. They communicate with other programs over the internet. Your site can ask the Google Maps server to show a map, the weather server to give the temperature, and the bank server to check the balance. All of this happens via HTTP API.
💡 Important note: HTTP API is not some single language or technology. It is simply a set of rules (protocol) by which programs communicate over the internet. JavaScript, Python, Java — they all know how to work with HTTP API, just the syntax is different.
Theory: how it works from the inside
Let's look "under the hood" and understand what actually happens when your program makes an HTTP request.
Client-server architecture
In any network communication, there are two sides:
- Client — the one who asks (your Node.js program)
- Server — the one who responds (a remote computer with data)
HTTP Request
When a client wants to get something, it sends a request that consists of:
- Method — what exactly we want to do (GET — read, POST — create, PUT — update, DELETE — delete)
- URL — the address where the resource is located (e.g.,
https://api.weather.com/v1/current) - Headers — additional information (data type, authorization, language)
- Body — the data we send (usually in POST or PUT requests)
HTTP Response
The server processes the request and returns a response that contains:
- Status code — a three-digit number that indicates whether everything is fine:
200— OK (success)201— Created400— Bad Request404— Not Found500— Internal Server Error
- Headers — meta-information about the response
- Body — the data itself (most often in JSON format)
Data format: JSON
Most modern APIs use JSON (JavaScript Object Notation). It looks like this:
{
"name": "Ivan",
"age": 25,
"hobbies": ["programming", "chess"],
"address": {
"city": "Moscow",
"street": "Tverskaya"
}
}
JSON is very similar to JavaScript objects, so working with it in Node.js is especially convenient — you can turn JSON into an object with one command.
Asynchrony
The most important thing to understand: network requests are asynchronous. When your program sends a request, it does not "fall asleep" waiting for a response. Instead, it continues to work, and when the response arrives, a special function (callback) processes it. In modern JavaScript, async/await is used for this, making the code look like regular synchronous code.
⚠️ Critically important: If you try to make an HTTP request without asynchrony, the program will either freeze or return undefined. This is one of the most common mistakes beginners make.
Syntax in JavaScript (Node.js): all forms and options
In Node.js, there are several ways to make HTTP requests. Let's look at all the main ones.
1. Built-in http/https module (low-level)
Node.js has built-in http and https modules. They do not require installation, but the code is cumbersome. They are rarely used, mainly for writing custom libraries.
2. axios library (most popular)
This is the de facto standard for HTTP requests in JavaScript. Convenient, simple, supports promises and async/await.
3. node-fetch library (modern standard)
Implementation of the browser's fetch for Node.js. Lightweight, modern, but requires installation.
4. got library (powerful and fast)
A newer library with a large number of features "out of the box".
Comparison of popular libraries
| Library | Installation | Popularity | Complexity | Features |
|---|---|---|---|---|
| axios | npm install axios |
⭐⭐⭐⭐⭐ | Low | Automatic JSON, interceptors |
| node-fetch | npm install node-fetch |
```