Skip to content

Instantly share code, notes, and snippets.

View mtorre4580's full-sized avatar
🏠
Working from home

Matias Daniel Torre mtorre4580

🏠
Working from home
View GitHub Profile
@mtorre4580
mtorre4580 / script.js
Created April 9, 2023 18:51
Example run executable script in Node.js
#!/usr/bin/env node
const os = require("os");
console.log('Information about CPUs \n', os.cpus());
// To run this script you need to apply the permision run executable -> chmod +x script.js
// The # allows to tell the shell run this script with node env
@mtorre4580
mtorre4580 / heavy_operation.js
Created April 9, 2023 18:29
Example fork process to handle intesive work load to avoid block the main process in Node.js
/**
* Example to simulate a intesive work, sum the value recieved
* @param {number} value
*/
const handlerHeavyOperation = (value = 0) => {
let res = value;
for (let i = 0; i < 90000; i++) {
res += i;
}
return res;
@mtorre4580
mtorre4580 / events.js
Created April 9, 2023 17:48
Example eventEmitter in Node.js
const EventEmitter = require("events");
/**
* Example using EventEmitter to notify custom events
* The class extends from EventEmitter to use the methods
* emit: Emit the custom event with the payload
* on: Handler to listen any custom event, similar to a eventListener in Web
*/
class Reviews extends EventEmitter {
constructor() {
@mtorre4580
mtorre4580 / require.js
Created April 9, 2023 17:31
Example check existence of a module in Node.js
/**
* What pass when require a module?
*
* Steps
*
* 1. Resolving
* 2. Loading
* 3. Wrapping
* 4. Evaluating
* 5. Caching
@mtorre4580
mtorre4580 / Dockerfile
Last active April 9, 2023 17:27
Example dockerfile for Node.js
FROM node:16.13.0-alpine AS base
FROM base AS build
WORKDIR /usr/src/app
COPY . .
RUN npm ci --production
RUN npm run build
FROM base AS publish
COPY --from=build /usr/src/app/package.json /usr/src/app/package.json
@mtorre4580
mtorre4580 / arguments.js
Created April 9, 2023 17:08
Example to retrieve the arguments in a node process
// Retrieve the arguments pass to the process
// Skip the first arguments (node path | file path)
const [, , ...params] = process.argv;
console.log("params recieved in the process", process.argv);
@mtorre4580
mtorre4580 / promisify.js
Created April 9, 2023 04:08
Example promisify util from Node.js to convert a callback to a promise
const { promisify } = require("util");
// Example legacy callback function with schema callback(err, response)
const legacyFunctionAPI = (name, callback) => {
setTimeout(() => {
if (Math.random() < 0.5) {
callback(null, { id: 2, name });
} else {
callback(new Error("The API has fail"), null);
}
@mtorre4580
mtorre4580 / cluster_app.js
Last active April 9, 2023 16:48
Example clustering mode in Node.js
const express = require("express");
const cluster = require("cluster");
const os = require("os");
const PORT = process.env.PORT || 3001;
// Replace with your logger
const logger = console;
// If the current is master, create the fork for all the cpus
@mtorre4580
mtorre4580 / error-handler.js
Last active April 9, 2023 02:44
Example error handler in node.js for APIs
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3001;
// Replace with your logger
const logger = console;
/**
@mtorre4580
mtorre4580 / http-client.js
Created April 9, 2023 02:32
Example http-client with circuit breaker support and keep alive connection with axios in Node.js
const axios = require("axios");
const Agent = require("agentkeepalive");
const CircuitBreaker = require("opossum");
// Replace with your configs
const TIMEOUT_DEFAULT = 2500;
const MAX_SOCKETS = 100;
const MAX_FREE_SOCKET = 10;
const TIME_OUT_KEEP_ALIVE = 60000;
const FREE_SOCKET_TIMEOUT = 30000;