May 13, 2018

JavaScript: Select Date Time with Timezone

This tutorial explains how to implement to get a JavaScript date object from user selected date time & timezone and the date object can be sent server side to save or for further processing. HTML5 provides input type datetime-local allows user to enter both a date and a time and the user’s local time zone is used. For simplicity, Let’s enhance this to allow user to select any timezone. We are going to use Moment-Timezone library.

May 13, 2018

JavaScript Async/Await: Serial, Parallel and Complex Flow

If you have experience on ASP.NET MVC then probably you are familiar with async/await keywords in C#. The same thing is now in JavaScript. Before that, Callbacks and Promises are used for asynchronous code. Async/await is built on top of promises and non blocking. The power of Async/await provides asynchronous code look like synchronous code. In this article, we will go through serial/chain and parallel flow and implement a complex flow using Async/Await.

Read Also: JavaScript Promises: Understanding Error Handling with Example

Feb 14, 2018

Visual Studio Code: Debugging ES6 Mocha tests with Babel

In my post, I explained how to test server side REST APIs in Node.js using Mocha, Sinon and Chai. I used ES6 test methods for this and Babel is used to execute it. As test methods are executed at development end, so NO need to transpile ES6 to ES5 first and then debug the converted files. You can directly debug ES6 code with Babel.

Feb 14, 2018

API Versioning, Express Routers and Node.js

Do you want to set up an API versioning in Node.js and Express environment? This tutorial explains the different ways to do REST API versioning step by step. We will implement the following routing for the versioning:

GET /api/v1.0/
POST /api/v1.0/
GET /api/v2.0/
POST /api/v2.0/

Feb 14, 2018

Node.js: Traversing A Directory Recursively with Limited Depth

In this post, we will implement to get all files recursively in a directory using Node.js. Also, will restrict it to traverse directory with limited recursively depth.

Before we get started, consider the following directory structure(For simplicity, the name is according to their depth level i.e. folder 1, sub folder 1.1, sub sub folder 1.1.1…etc):

1 > 1.1 > 1.1.1> >
2 > 2.1 > 2.1.1
  > 2.2

Each directory has txt file with same directory name(means 1.1 will have 1.1.txt file).

Dec 19, 2017

Node.js: Sinon Stub and Passport Authenticate

In my last post, I spent some hours to create a stub to skip passport.authenticate method for REST API testing. So, I decided to write a separate post on the same. Before we get started, let’s take a look on how auth middleware is implemented:

Dec 18, 2017

Node.js: REST API Testing using Mocha, Sinon and Chai

In my recent post, I covered how to implement token based authentication using Passport, JWT and bcrypt. Let’s extend this post and look into testing REST APIs or server side methods in Node.js using Mocha, Chai and Sinon.

Mocha: It is a test runner to execute our tests.
keywords in code = Describe, It, before, after…etc

Chai: It is an assertion library and allows to write code like writing regular english.
keywords in code = should, assert..etc.

Sinon: It provides test spies (fake functions to track executions), stubs (replace functions with our fake implementation) and mocks (predefined fake method with behaviour).

Dec 18, 2017

JavaScript: Null vs Undefined vs Undeclared

For a beginner, it is painful to understand two distinct things null & undefined in JavaScript and use them properly. I noticed many people consider and treat undeclared variable as undefined which is wrong perception. In this article, we will go through null, undefined, empty, NaN and undeclared variables and see how to handle them in different conditions.

Dec 17, 2017

Token Based Authentication with Node.js, Express, Mongoose and Passport

This tutorial explains how to implement REST API and Token based authentication in Node.js, Express, Mongoose environment. We are going to use JWT (JSON Web Token) + bcrypt (password hashing algo)+ Passport (authentication middleware to integrate different login strategies) combination. So jsonwebtoken, bcrypt-nodejs and passport-jwt javascript libraries will be used. It is assumed you are familiar with Node.js, MongoDB and ES6 basics.

Jul 17, 2016

Node.js: Simple TCP Server & Client and Promisify the Client

In this post, you will see an example of simple TCP server and client in traditional javascript way and in ES6 way. Node.js has net module which provides an asynchronous network API for creating stream-based TCP or IPC servers and clients. We are going to use it to implement TCP server and client. This post is updated with Node v6.11.2.

Jun 24, 2016

nvm-windows: Node or NPM not recognized after nvm installed

To manage multiple Node.js versions on windows, you are going to use and install node-windows. After installation, you run following commands to setup specific node version:

d:\>nvm install 4.7.2
d:\>nvm use 4.7.2
d:\>node --version 

and if you get following message
‘node’ is not recognized as an internal or external command, operable program or batch file.
then read this post to troubleshoot the issue.

Apr 10, 2016

Visual Studio Code: Setting Environment Variable for Tasks

Visual Studio Code (VSCode) is a lightweight, open-source code editor and available on Linux, Mac OSX, and Windows. One of the key features of Visual Studio Code is its great debugging support for Node.js (JavaScript and TypeScript) and another feature is to run Tasks(Grunt, Gulp, MSBuild…etc.) from the IDE. If you are new to VSCode, I would recommend to see following video which shows how to debug Node.js app, put break-point and use watch window, use environment variable in Visual Studio Code editor: