Beginners guide to installing and setting up MongoDB and GUI for MongoDB in Windows Machine
databases , MongoDB , Uncategorized / September 26, 2019

This is a step by step tutorial for installing MongoDB and setting up a GUI for MongoDB on Windows OS   Step – 1 Check whether MongoDB is already installed in the system To check whether MongoDB is already installed in the system, and the PATH variable is configured, use the following command. This will output the version of MongoDB .

The following command will output the Mongo shell version

If installed and configured, the output will be something like db version v3.4.14 –          mongod -version MongoDB shell version v3.4.14 –           mongo -version If not, the output will be like     Step – 2 Download and install  MongoDB and the mongo shell on Windows machine Go to the MongoDB official site and download the setup file for Mongo and install it on the machine. The default installation folder will be inside program files in your C drive

You can customize your installation and also choose whether to install or not install the MongoDB Compass which is an intuitive GUI by MongoDB Inc. Navigating to the bin folder and running the commands in Step 1 will give you the versions…

Promise.prototype.then() : JavaScript then() method and how to use it
Javascript , Requests , Uncategorized / September 25, 2019

Q:  How to use then() and how to return a promise from a function and use then() ?   A: then() is a method in the Promise prototype that returns a promise and accepts two callback functions. One function is for the success and the other is for the failure case of the promise. If both arguments are not given or are not callback functions, still then the method would not generate any errors. For example, here we create a new promise.

promise .then() exists hence it is a promise. .then() allows method chaining.   Now we will see how to use then to return a promise It is incorrect to use then() this way as it should return inside the then also. Otherwise resolving the promise returned by then will give undefined.

    It is incorrect to use the function without returning the promise returned  by then()

    Returning the promise returned by then is the correct way of returning a promise from a function

    If there is some kind of logic to be applied, this is the correct way of using then()

 

Node.js – tools/packages/practices for building a secure,high performance API with quality code

Contents >Code standard and styling >Security >Performance         In this article, we will take a look at some important aspects to be considered while developing Node.js applications and the tools/packages/practices that can be used to achieve them. Understanding and using these will help you in building a secure web application that is well structured and well documented and has a very high performance. Code standard and styling Tool – Linters(ESLint/JSLint/TSLint) Linters are static code analyzing tools that check for static code issues like styling errors, poorly structured code and unsafe code. Linters help in implementing a certain set of coding rules and helps in avoiding static errors like an absence of strict type checking, mismatched import or undefined variable usage .Static code analysis is analyzing the code for errors/faults in coding rule implementation before running the code. Dynamic code analysis is performed after the running. The most commonly used styling modules are Airbnb/JavaScript and Github/JavaScript.You have to integrate the linting plugin to the editor you are using(eg – Visual Studio Code/Sublime text). The linter will show linting errors like the following.   ESLint and JSLint are used along with JavaScript. TSLint is used with TypeScript. An example…

Check for duplicates in JavaScript Array of Objects
Express.js , Javascript , Uncategorized / July 28, 2019

How to check whether an Array of Objects contain duplicates by key Consider an Array of student Objects with each object having a key id. We have to perform a validation whether multiple objects with the same Id is present in the Array or not.

The above are Arrays of student Objects, one with duplicate id value and one without duplicate id value. To get the  Ids present in the Arrays we need to use the map() Method

  Now we have two Arrays containing all the Id values of Objects.  The first one has unique Ids  and the second one has duplicate Id 42342.   To check for duplicates in Arrays, we can create a set from the array. Sets will only have the unique items in the array

  The second set only has 2 values as one of the values in the Array had a duplicate. So, on comparing the length of the Array and size of the Set  we can determine id  Array has duplicates or not  

  The whole code to check whether an Array of objects contain key-value duplicates will look like this. We can use this logic…

Web Storage – A comparative study of Local Storage,Session Storage,Cookies,IndexedDB and WebSQL
databases , Javascript , Uncategorized / July 10, 2019

Storages in browsers – Web Storage Web Storage is the storage mechanism that allows Web applications to store data in Web Browsers. The various storage types include Local Storage, Session Storage, Cookies, IndexedDB, Web SQL. These storages are supported by most of the modern-day web browsers and are standardized by the World Wide Web Consortium. Web Storage lets a web application to store data which in turn helps in the effective end to end functionality of the application. Upon inspecting and navigating to the storage in the browser console, we can see what are the various storage mechanisms supported by the browser and data stored in them. FireFox       Chrome Local Storage Local storage is a key-value store that stores values as string. The data stored in local storage does not have an Expiration date and will stay in the browser forever until we clear the browser data using settings or using Javascript. Up to 10MB of data can be stored in most of the browsers and it is 10MB for Local Storage and Session Storage combined in chrome.IE has 10 MB each. Local Storage follows the same-origin policy. The same-origin means that the Protocol(Http/Https), port and the host…

Testing Node.js,Express.js API with Mocha and Chai – Typescript

Contents >What is Mocha >what is Chai >what is Chai HTTP >Installing dependencies and getting started >Writing tests in the test files What is Mocha Mocha is a Javascript testing framework working on both Node.js and in the browser. We can use mocha to test APIs written to run in Node.js, created using frameworks like Express. Mocha runs the test cases serially and provides very accurate reporting. Mocha makes testing asynchronous testing easy and efficient. The latest version of Mocha available to use is 6.1.4. To install Mocha to be used with Node.js, use

Mocha is used in the development and testing phases and ideally not used in a production environment. So you use –save-dev while installing Mocha. What is Chai Chai is an assertion library used with testing frameworks like Mocha. An assertion in testing is an expression that will be true at a specific state of the application if there is no bug in the program. While testing Node.JS APIs, we can use this assertion library to check whether the response received from an API endpoint for a specific request is the expected one or not. To the latest version of Chai available to use is 4.2.0. To install…

Redis Tutorial Part 3 – Redis Data structures and working with Node.js ioredis

Redis Data structures Key-value stores mostly support string keys and string data but Redis does a lot more than that. Redis is not just a simple key-value string store It supports complex kinds of data structures, not just string. We will look more into this property of Redis in this tutorial and see how to utilize this using Node.js and ioredis module. Get the ioredis module installed on your app from https://www.npmjs.com/package/@types/ioredis   1 String – Basic kind of string. Lets us say the name of a person. We will see how we can store this value using ioredis We set value bar into key foo using set and get the value using get.

2 Binary Safe String – Can contain any type of data represented as a string: PNG images or serialized objects, for example. Here we have a base64 encoded data of an image.We store this data in redis.Remember a value can’t be bigger than 512 MB. base64 of png – iVBORw0KGgoAAAANSUhEUgAAACMAAAAjCAYAAAAe2bNZAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gUfFBM7nAl7igAAAPVJREFUWMNj1G/R/88wSAATwyACLMgcYzljojXefHmT4cvPLwzU1A93TJRpFEOZWxnRhj37+IzBa4oXA7n6b7y8wRAxJwK7Y3g5eBkYGBgYNl7ayPD843O8BmXYZjBI8UuhiMH0Ews0xDXwRxMDAwPDpoubGM4+OkvQMcM+AY86ZtQxo46hSXXAwMDA4Kfvx2AibzKwjvn84zMDAwMDg7+eP9HVATKA6SelOkEHjMhNCFJC5NmHZxgGUqqfcbQ9M9qeobQ9QyzA1Z4htj1EVHuGUkBJe2i0Ohh1zKhjRkZ7hlJASXuIhVrtEUrbQ6PtmdHcRA4AAAq6hb9r4YF9AAAAAElFTkSuQmCC

We can store buffer data as well in redis..

3 Lists – Collections of elements ordered according to the order of insertion

    4 Sets – Sets of data. Here we have a…

Redis Tutorial Part 2 – Redis Sentinel and master/slave usage with Node.js ioredis

Redis Sentinel  System designed to help manage Redis instances. As we discussed earlier, one Redis instance can be master and other instances can be slaves. The slaves can have exact data copies of the master server. The master will have replication ID. The biggest advantage of this architecture is that the slave can act as a master if the master fails. Redis Sentinel is the system that helps to make Redis instances with High Availability (HA) by managing failovers. Redis Sentinel Does the following jobs Monitoring – Checking if your master and slave instances are working as expected. Notification – Notifying something went wrong. May be via an API. Automatic failover – If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master     The configuration file for sentinel is sentinel.conf  redis-server /path/to/sentinel.conf –sentinel   In the sentinel.conf file, there is a line like sentinel monitor mymaster 127.0.0.1 6379 2 quorum – 2 This line determines which master the sentinel is going to monitor. The number 2 is called quorum. The number determines how many sentinels need to agree that the master is having a Failover so that the slave…

Redis Tutorial Part 1 – Introduction

Redis – Remote Dictionary Server Redis (Remote Dictionary Server) is an opensource key-value store. It can be used as a database, cache and message broker. Redis is an in-memory store which means that it primarily relies on main memory/primary memory for data storage in comparison to other database storages like Mysql that relies on disk storage.   Redis is written in C language and it supports cross-platform. In-memory databases like Redis are faster compared to disk storage databases like Mysql. Redis is a key-value store. Which means that data is stored like name=’John’ The value john an be accessed using the key name. Key value is a string. Redis is single-threaded(One operation at a time) fast and scalable. The speed allows redis to be used as cache memory. Install Redis on Linux sudo apt-get update sudo apt-get install redis-server For Redis server redis-server For Redis Command Line Interface redis-cli To get configuration for redis goto redis-cli.type CONFIG type of configuration to get configuration values in redis.conf.   There are two processes in Redis architecture. REDIS-CLIENT – Manages client interaction REDIS-SERVER – Manages data storage in the memory and forms the major part of the Redis architecture. Single Redis instance can manage multiple redis…

Using Sequelize with typescript and express : Import all model files
Typescript , Uncategorized / February 17, 2019

STEP 1    Install Sequelize Install the latest version of Sequelize

Now create your migration script STEP 2  Initialize your project To create an empty project you  need to run init command

This will create seeders, config, migrations and models folders Modify the configuration file in your config folder accordingly   Step 3 Create your model and migration files

This will create Migration files in the migrations folder and model files in the models folder Step 4 Using models with typescript and express When you create multiple tables multiple model files will be created. To use all these model files at once, create a model folder in your project directory, create an index.ts file and include all the files using the fs module and path module of Node.js   The index.ts file will look like this  

The code

finds the model folder which is  outside your server files folder __dirname – Gives the current directory in which the executing file is located

Finds all .js model files inside the models folder

sequelizeconnection.import  import model files.In this case, it imports all model files

Above code adds the model to the model object

The…

Share this page in social media platforms