Decorators in Typescript
Decorators in Typescript

What is a Decorator?

It is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors (reference).

When we are using Typescript Method Decorators, they are higher-order functions that help us change method behavior or do something with the arguments.

Typescript method decorator definition: method decorator can be used to observe, modify, or replace a method definition (reference)

Now, let’s see how we can define a simple method decorator.

Setup

In order to run the Typescript code, we need to compile them using the Typescript compiler.

We need a tsconfig.json


Image for post
Image for post
First time using Deno

What is Deno?

A secure runtime for JavaScript and TypeScript.
Based on their website:
Deno is a simple, modern, and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust.

  1. Secure by default. No file, network, or environment access, unless explicitly enabled.
  2. Supports TypeScript out of the box.
  3. Ships only a single executable file.
  4. Has built-in utilities like a dependency inspector (deno info) and a code formatter (deno fmt).
  5. Has a set of reviewed (audited) standard modules that are guaranteed to work with Deno: deno.land/std

These were from Deno's official document.

Why Deno is created and who is the creator?

Deno is created by Ryan Dahl and he is the creator of NodeJS. He mentioned 10 things he regrets about NodeJS and he tried to solve those issues in Deno. This is the Video of the conference and I strongly recommend seeing this video cause it could bring a few questions into your mind. For example, why should we have a node_module or package.json? …


Testing NodeJS Express App
Testing NodeJS Express App
NodeJS Express Integration Test

What is an integration test?

“ Integration tests determine if independently developed units of software work correctly when they are connected to each other. The term has become blurred even by the diffuse standards of the software industry, so I’ve been wary of using it in my writing. In particular, many people assume integration tests are necessarily broad in scope, while they can be more effectively done with a narrower scope. “ Martin Fowler.

Testing an Express app

So as to see how we can test an Express app we need to create a simple Express application. I used MongoDB and its native driver(We usually use Mongoose as an ODM) but in order to keep it as simple as possible I didn’t use Mongoose also, I implemented users directly in app.js which is not a good practice but it helps us to focus on how tests are working. …


and one practical real-world example

JavaScript Closures in Plain Language
JavaScript Closures in Plain Language

What are JavaScript closures?

It is a JavaScript feature that allows a function to access its outer function scope. Sounds complicated?

We can use this feature when we have nested functions like this:

A simple example of closures
A simple example of closures
A simple example of closures

As you can see the function sayAge has access to the variable age which is in the outer function scope. This is a special feature of JavaScript and we can use it in some situations that I will explain.

A common trick with closures

If we return a function from another one we can mutate the outer function variables by using the closure feature. For example:

A common trick with closures
A common trick with closures
Changing the outer function’s variable

JavaScript closures are as simple as this example but I didn’t understand them till I found a practical usage. …


Image for post
Image for post
Datastream

What are streams?

Streams are a collection of data that might not be complete and we might not fit them in memory.

When we watch an online video we use a stream. Do we need to download the whole video to watch it? the answer is no. thus the video data is not complete but we can watch the parts we want!

Imagine we are uploading a video with 12GB size. The problem is our memory is only 4GB. So how is it possible to upload such a file? …


Debug files in Node.js without the use of ‘debugger’

NodeJs Built-in debugger
NodeJs Built-in debugger

Let’s imagine that I have a simple JavaScript file and I want to calculate the sum of integer numbers but I have a problem and I want to debug this file(but I don’t want to use console.log as a debugger!)

This is the file:

Image for post
Image for post
A simple function that we are going to debug.

In order to use debug mode we need to run the file with this command:

node inspect sumCalculator.js

Also, we can use this command but it is deprecated:

node debug sumCalculator.js

As we enter the debug mode we can see these lines on the console:

NodeJs debuge mode
NodeJs debuge mode
Nodejs debug mode

As we can see debugger is listening on port 9229 and a unique URL. We are going to use this URL after we learn a few commands in debug mode. …


If you are using git as a version control tool, you can learn the basics of Mercurial as easy as pie. I was wondering if their motto(Work Easier Work Faster) is true or not and I determined to do basic version control actions using mercurial just by using--help and it worked like a charm!

Image for post
Image for post

Installation

Doesn't need any explanation: sudo apt install hg.

If you are using other operating systems this link may help you.

If everything goes well you can see the version using hg --version

Config mercurial

After the installation, we need to add a user (just like Git) using this…


The “Event Emitter” is a Node.js core module that helps communication between objects

How we can use Event Emitter?

1. Import the module

const eventEmitter = require('events');

2. Create a class which extends eventEmitter

class myCustomClass extends eventEmitter {};

3. Instantiate from this class

const myObject = new myCustomClass();

4. Add event listener

myObject.on("myEventName", myFunction)

5. Emit an event

myObject.emit("myEventName");

Since we emitted this event our event listener invokes our function(myFunction).

This is the simplest way we can use an event emitter class.

Usage of Event Emitter

You know about the process one of the most important global modules that is available on the global object.

See this example:

A simple example of the usage of event emitters.
A simple example of the usage of event emitters.
A simple example of the usage of event emitters.

Sometimes we want to do something when the process is exited. When the process is exited an event emitted and we can add an event listener to it(Also you can use exit codes in your function). …


What the event loop is, how it works, and why it is important

What is I/O operations?

I/O stands for input/output. I/O is used to label a communication between a process in a CPU computer and anything external to that CPU(Memory, disk, network, or even another process) and the process communicates with these external things through signals. They are input when they are received by the process and they are output when they are sent by the process.
In Node.Js’s operations term, I/O refers to network and disk operations which are the most time-consuming operations. Node’s Event Loop is designed around the fact that the largest waste in computer programming comes from waiting for such I/O operations.

Handling slow I/O operations

  • Synchronous: Easiest way to handle slow I/O by executing operations one by one. …


We use require("someModule") to import a module in another file, but is happening under the hood to make require work?

The sequence of steps

  1. Resolving: get the absolute path of the module.
  2. Loading: load the module
  3. Wrapping: give a separate scope to your variables.
  4. Evaluating: is what the VM(usually V8)eventually does with the code
  5. Caching: cache the module in case we use it again.

What happens when we require a module?

To find out how requiring a module works, we can use or we type module in REPL mode. The module is available in the global object.

paths inside the nodejs`module`
paths inside the nodejs`module`
paths inside the `module`

If I use this line of code: const test = require("tetsModule") Node.js …

About

Poorshad Shaddel

Full Stack Developer at Telewebion.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store