Session Cookie Authentication in Node.js (With Complete Examples)

In this post, we will learn how to authenticate users using session cookies in a Node.js server application. When a user logs into our application, we need to know who they are across all our HTTP methods and routes. One way to do this is to store the users “session”. A session is started once a user logs in, and expires some time after that. Each logged in user has some reference to the session (called a cookie), which they send with their requests....

 · Soham Kamani

Using the Builder Pattern in Javascript (With Examples)

In this post, we will learn how to use the builder pattern in Javascript, and see some code examples, as well as advanced concepts like validation and fixed attribute options. If you just want to see the example code, you can view it on Github Basic Usage - Creating a Builder Class The builder pattern is a popular object oriented design pattern that makes it easier to construct object instances....

 · Soham Kamani

Making REST API calls with TypeScript (With Examples)

This post describes how to make API calls in Typescript, and how we can support types and interfaces while calling REST APIs. If you just want to see the example code, go here Typescript helps developers by providing static analysis and autocomplete features for their Javascript code. When calling REST APIs, the response is normally in a serialized format, most commonly JSON. We can then divide the process of integration API calls with Typescript into two parts:...

 · Soham Kamani

A Complete Guide to HTTP/2 in Node.js (With Example Code)

This post will explain what HTTP/2 is, and how we can make use of its features in Node.js. HTTP/2 is the next version of the Hyper Text Transport Protocol (HTTP), which adds many features and optimizations over the previous version. In the next few sections, we will understand these features in detail, and also learn how to implement them as a client and server using the Node.js http2 standard library....

 · Soham Kamani

A Guide On SQL Database Transactions In Node.js

This article explains what SQL database transactions are, and how to implement them in Node.js. SQL Transactions are very useful when you want to perform multiple operations on a database, but still treat them as a single unit. We will go into detail on why this is useful, and how you can use it in your Node.js applications. To illustrate our examples, we will use Postgres as our database of choice....

 · Soham Kamani

RSA Encryption, Decryption and Signing in Node.js (Javascript) - With Examples

This post will explain the RSA algorithm, and how we can implement RSA Encryption, Decryption and Signing in Node.js using its standard library. RSA (Rivest–Shamir–Adleman) encryption is one of the most widely used algorithms for secure data encryption. It is an asymmetric encryption algorithm, which is just another way to say “one-way”. In this case, it’s easy for anyone to encrypt a piece of data, but only possible for someone with the correct “key” to decrypt it....

 · Soham Kamani

Making a Testing Framework in Node.js (Without any External Libraries)

If you’ve ever written tests for a Node.js application, chances are you used an external library. However, you don’t need a library to run unit tests in Javascript. This post is going to illustrate how to make your own simple testing framework using nothing but the standard library. It’s actually much simpler than you may think. If you want to skip the explanation and just see the code, you can find it here...

 · Soham Kamani

Node.js File System Module - How to Read and Write Files and Directories

The Node.js fs module enables us to work with the file system, which is one of the most fundamental tasks when writing programs. However, there are several ways in which we can interact with the file system using the fs module, and each method has its trade-offs. This post will explain how to use the file system module to read and write files and directories using sync, async, and streaming APIs....

Last Updated:   · Soham Kamani

How to Set Expiry Time (TTL) for LocalStorage With Javascript

This post will explain how to implement expiry times for items stored in the browsers localStorage. If you’re familiar with the browsers localStorage object, you know that there’s no provision for providing an expiry time. However, we can use Javascript to add a TTL (Time to live) to invalidate items in localStorage after a certain period of time elapses. If you just want to see a working example, you can skip to the last section...

 · Soham Kamani

Implementing JWT Authentication in Node.js

In this post, we will demonstrate how JWT(JSON Web Token) based authentication works, and how to build a sample application in Node.js to implement it. If you already know how JWT works, and just want to see the implementation, you can skip ahead, or see the source code on Github The JSON web token (JWT) allows you to authenticate your users in a stateless manner, without actually storing any information about them on the system itself (as opposed to session based authentication)....

Last Updated:   · Soham Kamani