Latest in Servers and APIs

HomeServers and APIs

View summary of all Docker containers memory usage

You can get pretty much all resource usage information with docker stats, but what you will lack there is the summary.
However, with some command line magic you can get the summary. You can put this in a .sh script, and make it executable with chmod +x docker-memory.sh:
mem_amount_total_with_unit=$(docker system info \ | grep 'Total Memory: ' \ | tr -d 'Total Memory: ') unit=$(echo ${mem_amount_total_with_unit} \ | sed 's/[0-9\.]*//g') mem_amount_total=$(echo ${mem_amount_total_with_unit} \ | sed...

Simple load balancer with Docker, Nginx and Node.js

It is a common knowledge that Node.js is a single-threaded single-core process. It sounds like an issue, but it gives you full freedom over how many processes you can run and how many cores want to utilize.
There are several ways you can scale Node over whole machine: run multiple processes, use Node to run subprocesses, or use virtualization software. We will focus on the latter, and see how we can also load-balance requests between all of them with a quiet primitive Nginx configuration.
...

Detect user country by IP in Node.js

It shouldn't be hard to detect a user's country based on the IP address, right? Right... I really liked the simplicity of ip2location module: load the BIN file, get the location, and close the BIN file.
Prerequisites Before you install the module, you will need to download the BIN file by registering at ip2location and choosing the appropriate option. You will have a choice between the country, country + city, or other options....

Push instant updates with EventSource API

EventSource is a handy API supported by all browsers, which runs over the regular HTTP protocol and doesn't require setting up a separate WebSocket service. However, it has limitations, such as data being one-directional (server to client) or a connection being constantly occupied. Let's dive in.
The server We will start with setting up a simple server using one of the ways from the previous article. After you have the server, we will set up a separate...

5 ways to create an HTTP/HTTP2 server with Node.js.

Node.js can be used to easily run a web server by using the built-in HTTP module or a web framework such as Express, Hapi, or Fastify. The process involves creating an HTTP server, setting up routes, and starting the server to listen on a specific port for incoming requests.
1. Using the built-in http.createServer The easiest and most typical way is to use the built-in http module.
const http...

fetch() with timeout

Being from early JavaScript days, you might've been used to setting a timeout for all your HTTP requests in jQuery or libraries like axios, request, etc. Yet to your surprise, there's no timeout parameter for native browser's fetch.
However, the way fetch extends on this, is now it allows you to set a different condition to abort the request. This can be not only the timeout, but any other condition (i.e. user logs out of the system, and all pending requests have to be...

Automatically serve .webp or .avif with Node.js based on browser request headers.

Let's chew down another issue: we have a /public folder with images, and depending on the browser we want to serve a WEBP or AVIF to save bandwidth and decrease page load time.
We will go through steps required by setting up a simple Express (Express.js) server. The setup will be very minimal, however, it will be a fully working server. Let's start by creating a project folder and creating an empty package.json with my favorite echo {} > package.json. After that, add the...

How to create a placehold.it image generator clone with Node.js?

What if I tell you generating images is actually an easy task, and you can create a simple placehold.it clone with only a bunch of lines.
Installing dependencies Let's start with the basics. We will start with express and sharp and create a very very simple setup:
import express from 'express'; import sharp from 'sharp'; const port = 8000; const app = express(); 
2025 Code with Node.js