NodeJS Tutorial Part One (Introduction)

What is NodeJS?

NodeJS is a scalable network application builder that uses an asynchronous event-driven JavaScript engine. NodeJS is a free programming language. It is an open-source Chrome’s JavaScript runtime environment, allows you to develop scalable online apps with ease. This environment is built on top of Google Chrome’s JavaScript Engine V8. It uses an event-driven, non-blocking I/O model, which makes it lightweight, efficient, and ideal for data-intensive real-time applications that operate across several devices. NodeJS runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).

Before we go any further, there are two things you should be aware of,

  • NodeJS is a programming environment, not a programming language or framework.
  • Contrary to common belief, NodeJS may be used on both the front and backend of a web application.

Benefits of Using NodeJS

  • The ability to scale up quickly: In NodeJS, each node is based around an “event.” For instance, a customer could make an in-app purchase or send an email to customer service. There’s almost no limit to how many nodes you can add to your basic programming function. This means you may scale vertically by introducing new capability paths back to your application’s core code. You may also scale horizontally by adding new resources into existing code. In either way, scalability allows your application to expand, which is one of the main advantages of using NodeJS.
  • Speed and Performance: The environment’s non-blocking input-output operations make it one of the fastest solutions accessible. Code executes rapidly, which improves the overall run-time environment. This is largely due to its sectioned-off system. But it also has something to do with the Google V8 JavaScript engine. Its programs are more likely to be written in JavaScript from end to end, and the plug-and-play compatibility helps with speed and performance.
  • The accessibility of a single programming language: Programmers may easily tie nodes into the rest of the full-stack development because it is powered by JavaScript. This makes it easier for front-end developers to take on more challenging back-end programming tasks. There are no other server-side languages required. That is a positive thing since it speeds up development in almost every area and allows programmers of all levels easy access to the back end of your mobile apps. It is one of the most important benefits of NodeJS.
  • Easy to master: NodeJS is easy to learn, primarily because it’s based on JavaScript. This makes it easier for your front-end programmers or designers to learn more complicated server-side programming with ease.
  • Real-time communication: It maintains a steady connection between the user and the server, ensuring that there is no lag between what the user requests and the server’s response. While other run-time systems attempt to interpret a message letter by letter, it has already completed the request in its entirety. It is also worth noting that it supports Websockets, one of the most widely used real-time communication solutions in the market. You may ensure that your users are constantly in sync with you by using NodeJS features like this one.
  • Cross-platform development
  • Robust technology stack
  • Fast-processing and event-based model
  • Rich ecosystem
  • Strong corporate support
  • Seamless JSON support
  • Flexibility
  • Efficient caching
  • Fast-to-market-development
  • An active user community
  • Efficient Queueing of Requests
  • A better choice for mobile
  • Node Package Manager for Enterprise

Disadvantages of Using NodeJS

  • Inability to process CPU bound tasks quickly: Because it processes JavaScript, which is also single-threaded, it is deemed single-threaded. Its non-blocking input/output approach employs an event loop to asynchronously handle threads. And everything is OK until Node is assigned a CPU-intensive task. It gives priority to these CPU-intensive operations first, resulting in delayed processing and overall latency in the event loop. This is why many programmers believe Node.js is unsuitable for programs that demand a lot of computing. It’s worth noting that as part of the 10.5.0 upgrade in 2018, it added a multi-threading capability called worker threads. This module may make use of extra threads from a thread pool, allowing for the execution of demanding parallel tasks on multiple threads. It’s currently considered experimental, but it might help to mitigate this evident problem in the future. Still, it’s one of Node’s advantages and disadvantages to consider carefully.
  • Unstable API: Its Application User Interface has changed multiple times. Not all of the modifications are compatible with previous versions. That implies developers will have to make changes to the exposed code bases only to be compatible with the current NodeJS API version. It is not always the best situation.
  • Callback hell issue: Because of its asynchronous nature, Node.js mainly relies on callbacks, which are routines that execute whenever each job in the queue has completed. Keep an eye out if you have a lot of queued jobs operating in the background. You might end up in “callback hell,” as Node engineers call it, which has a direct influence on code quality. Callbacks become buried many layers deep as a result of this.
  • Hot competition for Node programmers
  • A weak library system
  • Performance bottlenecks with heavy computation tasks
  • Immaturity of tooling
  • An overgrown, yet immature npm module registry

Features of NodeJS

  • Asynchronous and Event Driven: The APIs of the NodeJS library are all asynchronous (non-blocking). A server built in NodeJS never has to wait for data from an API. The server moves on to the next API after accessing one. It uses a notification system called Events to receive and track replies to previous API requests.
  • Very Fast: When data is sent in numerous streams, it takes a long time to process them all. NodeJS is a rapid data processing framework. It concurrently processes and uploads a file, which saves a lot of time. As a result, NodeJS boosts data and video streaming performance.
  • Single Threaded but Highly Scalable: NodeJS uses a single threaded model with event looping. In contrast to traditional servers, which create limited threads to handle requests, the event mechanism allows the server to respond in a non-blocking way and makes it highly scalable. When compared to conventional servers like Apache HTTP Server, NodeJS uses a single threaded application that can handle a considerably higher number of requests.
  • No Buffering: Data is never buffered in a NodeJS application. The data is simply output in chunks by these apps.
  • Uses JavaScript: The NodeJS library makes use of JavaScript, which is another key feature of NodeJS from an engineer’s perspective. JavaScript is already familiar to most of the engineers. As a result, working with NodeJS will be lot easier for a designer who is comfortable with JavaScript.
  • Cross-platform compatibility: NodeJS is compatible with a wide range of operating systems, including Windows, Unix, Linux, Mac OS X, and mobile devices. It can be used in conjunction with the proper package to generate a self-sufficient executable.
  • License

Installation

Prerequisites

  • A user account with administrator privileges (or the ability to download and install software)
  • Access to the windows command line (search > cmd > right-click > run as administrator) OR Windows PowerShell (Search > Powershell > right-click > run as administrator)

How to Install NodeJS and NPM on Windows

In a web browser, navigate to https://nodejs.org/en/download/. To download the latest default version, click the Windows Installer button. Version 16.14.0 -x64 was the most recent version at the time this article was published. The NPM package manager is included in the NodeJS installer.

  1. Once the installer finishes downloading, launch it. Open the downloads link in your browser and click the file. Or browse to the location where you have saved the file and double-click it to launch.
  2. The system will ask if you want to run the software — click Run.
  3. You will be welcomed to the NodeJS Setup Wizard — click Next.
  4. On the next screen, review the license agreement. Click Next if you agree to the terms and install the software.
  5. The installer will prompt you for the installation location. Leave the default location, unless you have a specific need to install it somewhere else — then click Next.
  6. The wizard will let you select components to include or remove from the installation. Again, unless you have a specific need, accept the defaults by clicking Next.
  7. Finally, click the Install button to run the installer. When it finishes, click Finish.

Open a command prompt (or PowerShell), and enter the following:

node –v

The system should display the NodeJS version installed on your system. You can do the same for NPM:

npm –v

How to Update Node.js and NPM on Windows

One option to update your NodeJS up to date is to go to its official download page and download the latest version. Bz doing so, the system should overwrite the older version with the updated one.

  1. Navigate to the NodeJS website and click on the latest stable version or the newest current release (with the latest features).
  2. After deciding the version, click on the Windows. The system downloads the package and stores it in the specified directory.
  3. Once the download is complete, run the installer.
  4. The NodeJS Setup Wizard appears and guides you through the installation.
  5. Accept the License Agreement by checking the box and click Next.
  6. Choose the destination folder where you want to install NodeJS.
  7. NodeJS allows you to select how you want to install the Node features. Change the way the features are installed by clicking on the icons in the tree.
  8. With that, the latest NodeJS is ready to install. Click Install to confirm, wait until the installation completes, and click Finish.
  9. Check the NodeJS version with the command:
    node –v

If you want to upgrade NodeJS from the command line, use the n model within the npm command. The n feature allows you to interact with different NodeJS versions.

  1. Before updating the NodeJS release, check which version you are currently using with:
    node –v
  2. Next, clear npm cache with the command:
    npm cache clean –f
  3. Install n globally:
    npm install –g n
  4. Now that you have n installed, you can use the module to install the latest stable release of NodeJS:
    sudo n stable

How to Uninstall NodeJS and NPM on Windows

You can uninstall NodeJS from the Control Panel in Windows.

To do so:

  1. Click the Start button > Settings (gear icon) > Apps.
  2. Scroll down to find Node.js and click to highlight.
  3. Select Uninstall. This launches a wizard to uninstall the software.

That concludes part one of this NodeJS tutorial. We’ve looked at NodeJS briefly in this blog. If you want to learn more about NodeJS, visit its website, find it on Github or check out this repository of examples.

In the next part of this tutorial, we’ll learn more about Node and how to build a CRUD application. Leave a comment below if you have any questions or comments.

--

--

Trainee Software Engineer at Koko | Software Engineering Undergraduate at SLIIT

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hansaka Dilshan Jayawardana

Hansaka Dilshan Jayawardana

1 Follower

Trainee Software Engineer at Koko | Software Engineering Undergraduate at SLIIT