Skip to content

Instantly share code, notes, and snippets.

View SeLub's full-sized avatar
🎯
Focusing

Sergey Lubimov SeLub

🎯
Focusing
View GitHub Profile
@SeLub
SeLub / cluster.md
Created October 2, 2025 10:42
How Clustering Works in Node.js

When using clusters in Node.js, it's important to understand how clustering works and whether or not a load balancer is necessary. Clustering allows you to create multiple server instances that share the same port, which can be beneficial for utilizing multi-core systems. However, this does not inherently provide any load balancing capabilities on its own.

How Clustering Works in Node.js

When you use cluster module in Node.js, it forks several child processes to handle incoming connections. Each fork listens on the same port and can receive requests for different parts of your application. Here's a basic example:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

How to restrict API access to our backend

Here are several approaches to restrict API access to our frontend:

CORS Policy Configuration:

import cors from '@fastify/cors';

fastify.register(cors, {
@SeLub
SeLub / how_to_strucure_nodejs_project.md
Created February 8, 2025 09:59
How to structure project

How to structure NodeJS project

Possible structures:

File structure

1. Domain-Driven Structure:

src/
├── domains/

Binary Search Task

Task: We have an array sorted in ascending order:

a = [ 3, 4, 6, 9, 10, 12, 14, 15, 17, 19, 21 ]; Define a function f(a, x) that would return x, the nearest smallest number, or -1 on error.

Example:

Task

console.log '1' after 1 second,
'2' after 2 seconds and
'3' after 3 seconds after start of the program.

setTimeout

The delay parameter only guarantees that your callback will not execute before that time has elapsed. Other queued tasks or operations could delay it further than the specified time.

const startTime = process.hrtime();
@SeLub
SeLub / gist:f3d61ae628d500e022f28822ad611127
Last active February 8, 2024 06:23
Remove or recover last commit pushed to git

Delete last commit from git

First way

git reset --hard HEAD~1
git push --force

Another way