Build your nodeJS Application in a modular way

Now a days almost all web services or integrations are done on top of nodeJS run-time. NodeJS is a flexible platform with lot of community support. Even it is possible to create documents like xlsx, docx or pdf directly from nodeJS. All major cloud platforms uses nodeJS as their level 1 language.

Modularity

NodeJS by design achieves the modularity by using the node_modules structure. All the required modules are stored in node_modules directory and we can invoke the modules any where in our code.

But are we using this modularity in our application code. Most of the application I saw contains a lib folder, i which we store all js files . This js files are imported in the required areas using relative paths.

 

The main problem with this kind of approach is that when we change the path of a service file the path to the db should change. Also the format is not readable. We will be confused with the file’s authenticity.

The Solution

A much better approach is to design our application as modules, such as db , logging, error etc . Lets say your application name is cms, then it is much easier to represent the module using scope

 

 

You can develop the modules separately and publish in to any NPM servers ( public / private ) and use it as like any other module.

If your application needs the logging module

 

If you don’t want to split your application in to bits and pieces, there is another approach.

A more better way

Keep the modules that you want inside a separate folder. Lets say “@cms”. Use separate folder for each module and let the modules have a separate package.json. This way it will become a valid node module.

The package json for the modules will look like this

 

Once the modules are ready now its time to do some scripting. Add the install.js in the “scripts” folder.

 

Add this script to your main package.json.

 

The script will be executed every time you do npm install. So once all other node modules defined in the dependencies are installed, it will create a link from the @cms folder outside to the @cms folder inside node_modules. So any changes you make to the outer @cms folder will be reflected to the folder inside node_modules.

 

You can see that the symlinks are installed for @cms. This is not a shortcut file rather that hard links created using “ln” in linux.

node_modules folder
@cms folder is along with other npm modules in the node_modules directory

Inside @cms you can see our modules which is defined in the outer @cms folder

The modules inside @cms scope

This way you can achieve modularity. “@cms” folder is part of your source code. You can then import the required modules in the normal way.

When you want your application to execute run “npm install” followed by “npm start“.

This approach helps me in making the application more modular and extensible. Let me know your thoughts on this.

JWT – Security with Simplicity

Reading time : 1 minute

In today’s world , security is the prime priority.

Throughout the evolution of computers, security also evolved from simple password based authentication to multi factor based authentication, from simple text encryption to two key encryption.  The latest in this evolution is JWT (JSON Web Tokens), which handles secure message passing between two clients / parties.

Why we need JWT ?

In a normal token based authentication, a token is granted for a authenticated user. All the subsequent service calls uses this token and identifies the user. The identification process takes place at server where this token is validated against a token store data. Same is the case for a SAML authentication. The idea of JWT is to make this token self contained with out loosing the compact nature. So the identity will also lies with in the token . This will reduce the time to look up in the token store and also makes the program code straight forward.

Continue reading “JWT – Security with Simplicity”

KMA management convention

It was a wonderful experience attending the convention. Had many experience sharing sessions. Had received a new view of life, attitude and management. 

My experiments with IoT – “Meeting with Pi”

Ingredients:  Little bit of nodeJS, ½ teaspoon raspberry pi. 🙂

Reading time : 3 minutes

When I heard the word IoT from one of my colleagues back in 2012, it was just like a piece of tech which seems to be very far from my world. Later when I started working with Arduino and raspberry pi in 2015, I realized the potential.

How it started

I am a graduate in computer science, with very little interest in electronics. During my college days, we had a paper on electronics and my score is “just pass”. My prime focus was behind programming languages and its concepts. Tried to learn many programming languages during the college days itself. It seems very easy for me to learn new languages as I tried to understand the concepts first, then the constructs.

It was in 2015 when one of my colleagues explained me about the working of raspberry pi. I was quite interested in it and there began the new phase of my learning.

Continue reading “My experiments with IoT – “Meeting with Pi””