Data validation in NestJS using “Pipe”

Introduction to Nest.js “Pipe” Pipes in NestJS are special components used to transform and validate data before it reaches the controller or controller methods. Pipes are useful when we want to ensure that the data we receive in API requests meets certain conditions or is transformed in a specific way. Nest.js comes with built-in pipes … Read more

Introduction to Nest.js

What is Nest.js? Nest.js is a Node.js framework for creating server-side applications. It uses modular architecture, which means that the application is divided into modules, where each module groups related functionality, so that new functionalities can be easily added without disrupting the existing parts of the application. Nest.js uses the TypeScript language (although JavaScript can … Read more

Environment variables = ENV variable

What are environment variables Environment Variables (Environment Variables) are used in various places in software development and IT infrastructure, such as code: operating systems (“PATH” variable that defines the directories in which the operating system searches for executable files), software development (variables for storing configuration parameters such as API keys, databases, URLs..), security (for storing … Read more

Node starter project (TypeScript + Docker)

Project skeleton We will assume that the project will have several smaller projects, ie. container, e.g. a part for microservices, a part for working with the database… It is good to create subfolders in the root of the project for each smaller project, e.g.:

Creating package.json First we will create a package.json file in … Read more

Object-oriented programming

What is object-oriented programming? Object-oriented programming or “OOP” for short is a way of organizing code around smaller units called “objects”. Object-oriented programming helps structure code into understandable and self-contained units that can be easily reused in different parts of the program. Applying object-oriented programming results in a more transparent and understandable program code that … Read more

What is a Socket?

Socket “Socket” represents an end point for sending or receiving data in a network. In other words, it is the interface between the application and network communication on the operating system. Through sockets, applications can communicate with each other over the network. Sockets are the basis for many network applications and services, including web browsers, … Read more

Creating a server with Node.js

Creating a server without a library To create a server, you need the http/https object, which is built into the core of node.js, and we can import it as a module. There are two module options: http – Http is one of the built-in modules that comes with Node.js and allows creating a server to … Read more

Introduction to Node.js

What is node.js? JavaScript is a programming language that cannot be used independently, but for its execution a special environment is needed, the so-called. “JavaScript runtime environment”. The most famous “JavaScript runtime environment” is the browser, but if we want to execute JavaScript outside the browser, node.js appears on the scene as another “JavaScript runtime … Read more

Exponential operator **

Operator syntax Until the ES2016 standard, there was no exponential operator in JavaScript, but the method Math.pow was used. Now the “**” operator returns the result of scaling the base (first operand) with the exponent (second operand): <strong>x<sup>y</sup></strong> = Math.pow(x, y) = <strong>x**y</strong> Example

Example In addition to this standard operator, we can use it with the operator “=”: … Read more

Iterators & Generators

Iterable protocol

Protocol characteristics

The

“Iterable protocol” is an agreed-upon APi at the level of JavaScript as a language, for creating objects that can be used to iterate over a collection of data. The key characteristic of this protocol is “sequentiality” ie. property that when iterating, it returns the value of the iterable structure one by one. Data types that satisfy the iterable protocol are called “iterable collections”. Respecting the iterable protocol, it is necessary that the function returns an object containing a special method “next()”.

next()

When called, the next() method returns a member of the collection temporarily “wrapped” (eng. wrapped”) with an object that has two properties: value and done. As long as there are members for iteration, the method returns the value of the “done” property as “false”, and only when the iteration “comes” to the end, the “done” property is defined as “true”.

Example

Through the following example, the concept of the protocol:

is described

Which data types are iterable?

It should be emphasized that ordinary javascript objects are not iterable, but the following data types satisfy the iterable protocol:

  • String
  • Array
  • Array-like arguments or NodeList object
  • TypedArray
  • Map
  • Set

Which mechanisms in JavaScript require an iterable collection?

Mechanisms for the use of which “iterable” collections are necessary are:

  • Destructuring
  • for..of loop
  • Array.from()
  • Spread operator ()
  • Maps & Sets
  • Promise.all(), Promise.race()
  • yield*
Example

Iterable collections can use mechanisms that require iterable collections such as iteration with a “for..of” loop:

or when destructing:

Read more