Getting started with Node.JS

Malav Live
5 min readMay 4, 2020

--

Hello Everyone,

I hope you guys enjoyed my previous article about the MVVM with iOS, if not do check out and let me know your feedback in comments.

This article explains my understanding as a mobile app developer's perspective towards backend development.

Background of Node JS

Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a web browser.

JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. While it is most well-known as the scripting language for Web pages and to take it further NodeJS comes in the picture, it’s the server-side JavaScript runtime environment and it’s built on top of the Google Chrome V8 JavaScript engine.

Apart from that, it’s an asynchronous event-driven JavaScript runtime which means its running single thread which accepts the request, process it using queues, and replies back when results are ready with success or errors without being blocked or not accepting the newer requests. Which indicates it’s super scalable.

Usecase of Node.JS

  • Real-Time Apps
  • Data-Intensive Apps
  • To build Scalable Backend
  • Non-Blocking Asynchronous Thread

However, Node.JS can not be used for CPU intensive operations like audio or video processing, because it has only one thread that handles the request, and CPU intensive operations take a huge amount of time, furthermore, other requests have to wait to until the large operation finishes.

Node JS + Mongo DB — A deadly combination

MongoDB is one of the most powerful no SQL databases with the features of SQL. This means without having the knowledge of complex SQL queries we can easily make the rich APIs that can store the data as documents in JSON format in different collections ( Tables in SQL) and since it’s kind of a JSON file, it’s easy to find the data with different parameters.

Impressive ways to store information in MongoDB as collections

  • Embed Documents
    Not like SQL where we always have to create a separate table for related information, We can easily embed a document inside a document which reduces the query time to during the fetch requests.
  • Collection References
    Collection in node do support references as well where we can add objectId as a reference of different collection and it’s easy to retrieve the content based on reference and again it requires no knowledge of SQL Joins.

Embedded documents are useful to store the data which is not going to change in the future. For instance, We can have a collection containing all the gender information and another collection containing user’s gender information with other information, so here the original gender collection is not going to update it’s existing records so, we can embed this information in user’s collection itself. This will also reduce the response time of API as now we are looking at the one collection only instead of two.

Collection References are useful to store information based on the user’s interaction inside the application. For example, If a user subscribed to an event then we should use the reference to the events collection as information of event can be changed by the event organizer and that should be visible to users too.

NPM ( Node Package Manager )

NPM is a package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.

Just like the cocoapods in iOS development which used to add different frameworks and libraries to the app, here we have NPM which used to install dependencies and packages.

Let’s checkout basic terminal commands of it.

  • Install a package
    We can install a specific package by running npm install package_name
    We can also install package for development purpose only and to do so we can execute a command like npm install package_name --save—dev
  • Update a package version
    We can call npm update to update all the packages from package.json
  • To Run the Node Application
    we can all node script_name.js usually, it’s always index.js file that sets up the application and loads required modules.

Node JS + MongoDB + Express JS

Express.js, or simply Express, is a web application framework for Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs. It has been called the de facto standard server framework for Node.js.

Basically, express.js simplifies the process of server creation and functionality related to it. Like listening to requests from a specific PORT of the server.

As we can see, when we import the module by calling the require method, it returns the express class. We can create an instance of the class and use it further in different modules by exporting it or pass as arguments.

By simply calling the listen method of express, the server starts listening requests from PORT 3000.

Ways to perform operation asynchronously

Node supports both ways asynchronous and synchronously operation to perform but it’s always good to use asynchronous methods to keep the thread available to serve more requests.

  • Callback Function
    Callback functions are like the closure or block in iOS where we call a method and closure function passed as arguments with it which will be called when the operation finishes asynchronously.

The only problem with this callback function is when we have more than one async operations to be performed then we will have to create a nested code where based on the result of 1st operation, the next operation block will be called and so on which makes code messy.

  • Promises
    - Promise solves the problem of nested callbacks.
    - Promises can be resolved or reject by the function and it can be handle by then block for the response and catch block to handle errors thrown by the functions.

We can even call the multiple promises and handle errors with one block as well so if any of the processes failed, it will not call the further functions and returns the error.

Also, looks cleaner as compare to callback functions.

  • Async and Await
    -
    Async and await help to write asynchronous code which looks like synchronous. For simple queries and data manipulation, Promises can be useful, but if you run into scenarios where the complex data processing involved, it’s easier to understand by looking at the code only with async and await functions.
    - Internally, it uses promises and returns the result if success or error if failure.

As we can see in the above code that function is marked as async which makes sure the execution will wait for the results of the await process and if any operation throws error catch block is there to handle it.

That’s it for now…

I hope you guys enjoy the reading and have an overview of the NodeJS apps.

Thanks for reading my article

If you found this interesting, do check out my next article about the Architecture of Node JS Application.

Happy Coding….!!!

--

--