What Can Nodejs Do?

0
41
what can nodejs do?

What can Nodejs do? NodeJS is a runtime environment for using server-side JavaScript. Thanks to its non-blocking operation, it makes it possible to design high-performance network applications, such as a web server, an API, or a CRON job.

Created in 2009 by Ryan Dahl, this JavaScript runtime was primarily intended to address the limitations of sequential programming and web servers (such as Apache HTTP Server). These technologies effectively reached their limits when they had to manage many simultaneous connections.

Node.js, a useful and efficient platform

The solution provided by Node.js is based on three fundamental bases:

  • The JavaScript V8 engine developed by Google, which allows running JavaScript code inside Google Chrome and, thanks to Node, directly on the server.
  • An event loop, also called NodeJS event loop, which allows the execution of several simultaneous operations in an asynchronous and non-blocking pattern by taking advantage of the multiple threads of execution (multi-threading) of modern processor cores.
  • A low-level-based API on the input-output (I/O) structure called libuv allows the adoption of event-driven programming.

If the adoption of Node JS had gradually reduced over time, the arrival of the ES6 syntax (ECMAScript published in 2015) has considerably boosted its popularity, in particular thanks to the Promises concept, which was able to put an end to the “callback hell” in the code.

const verifyUser = function (username, password, callback) {
  database.verifyUser(username, password, (error, userInfo) => {
    if (error) {
      callback(error)
    } else {
      database.getRoles(username, (error, roles) => {
        if (error) {
          callback(error)
        } else {
          database.logAccess(username, (error) => {
            if (error) {
              callback (error)
            }
            callback (null, userInfo, roles)
          })
        }
      })
    }
  })
}

The code snippet above is an example of callback hell where asynchronous management is embedded in each function, making reading and understanding the code complicated.

const verifyUser = function (username,password) {
  database.verifyUser(username,password)
    .then(userInfo => database.getRoles(userInfo))
    .then(rolesInfo => database.logAccess(rolesInfo))
    .then(finalResult => {
      // logic to be implemented on the verified user
    })
    .catch(err) => {
      // If there is an error
    }
}

Above is the same code using promises. It is much more pleasant to read and understand.

In 2018, Node.js exceeded one billion downloads and has become one of the most used technologies for developing modern applications, especially by many technology leaders such as Amazon, eBay, Paypal and Reddit.

In this guide, I will try to explain in detail what nodejs can do hoping you will learn something fascinating about this server-side language.

Why learn Node.js?

The world of web development is constantly changing and new solutions can become obsolete as quickly as they become trendy. After more than ten years of trial and error, Node.js still stands out as an exception and has surpassed the status of a solid alternative to a more traditional stack such as Apache/PHP.

Related: Nodejs and MySQL connection (complete guide)

If a developer ever asks me the question “ why learn Node.js? ”, the first 3 things that will come to my mind are:

  • the use of JavaScript
  • the state of the job market
  • and the learning curve.

Node is JavaScript-based

Created in 1995 by Netscape and long confined to making small animations on websites, JavaScript is today the most popular web development language in the world. This can be illustrated by several statistics including the following:

  • JavaScript is the most used language by respondents to the 2019 Stack Overflow Developers Survey (source)
  • It’s also the most prominent language in GitHub contributions for at least five years (source)
  • Finally, JavaScript is the language used by an overwhelming majority of websites for the front end but is also increasingly popular for the back end (source)

These reasons make JavaScript an essential language and its use at the base of Node.js is a definite advantage because all web developers already know at least the basics. There is thus a large community of JavaScript developers driven by an open-source mentality. Thus, promoting knowledge exchange and skills.

The job market is in demand

The advantages provided by Node.js (acceleration of development, scalability, flexibility, ease of creating full-stack teams, etc.) make it a solution that is increasingly present in company projects around the world.

As the adoption of NodeJS in enterprises grows, the demand for developers proficient in this technology in the job market also increases.

For example, the recruitment platform specializing in IT Hired announced that in France, Node developers registered on its platform received an average of 6.5 interview offers per month in 2019 compared to 4.2 for Android developers, for example.

In 2020 and still, in France, the NodeJS developer market is even described as “highly penurious”, making full-stack JS developers the highest-paid developers.

The learning curve is affordable

Node JS is a technology that is quite easy to learn, especially thanks to the many frameworks available that simplify development. If it also has certain weaknesses (especially for client-side execution), JavaScript remains a versatile language that can meet many needs. It is also easy to learn, especially since the arrival of the ES6 syntax.

The growing community of Node and JavaScript developers also facilitates learning by constantly providing new resources (courses, tutorials, videos, etc.)

The use of JavaScript, the high demand in the job market, and the ease of learning are the main reasons that push developers to learn this technology.

Node.js: what can nodejs do?

Node JS is so popular today because it has many advantages over more traditional languages ​​and technologies.

By offering basic functionality to be supplemented by modules via npm, it is extremely flexible and versatile.

Indeed, as we will see below, it is suitable for many types of projects.

Node is very suitable for web servers

NodeJS is single-threaded: it only runs on a single instance (a thread or thread of execution) of the operating system, unlike a server running in PHP (with Apache HTTP Server for example ) which is multi-threaded.

This feature of Node allows it to manage requests in a non-blocking way. Where a PHP server mobilizes a thread to receive a request and waits for it to pass through all the code to return a response to the client, the Node server will handle each request asynchronously within a single thread. The request is thus received and then sent to the callback queue where the thread serves the responses one by one.

Some Node libraries are multi-threaded: the libuv used as the basis for Node.js allows you to set up a thread pool containing several threads, each assigned to a processor core. This multiplication of execution threads makes it possible to optimize the execution time of each thread. Node is still single-threaded but uses multiple threads background to execute code asynchronously using the libuv .

This asynchronous management of requests makes NodeJS very suitable for input/output, that is to say, the vast majority of requests on the web (search, display, insert, modify, delete information on a database, etc. ).

Using JavaScript as a server-side programming language also breaks down barriers between the front end and back end, making it easier to build a full stack. This ability to build complete applications in a single language is also one of the reasons for the growing adoption of Node in the enterprise.

NodeJS and Command Line Interfaces (CLI)

PHP, Python, and C are equally subject to competition from Node.js, this time in the field of command line interfaces (CLI). Node makes it possible to build such tools by taking advantage of the hundreds of thousands of modules available on npm.

A plethora of very popular packages are indeed dedicated to CLIs, among other things, creating inputs (for example inquirer, prompts or email-prompt) or even improving the terminal graphically (boxen, ora, listr ).

From 2018, fledged frameworks such as oclif or gluegun appeared and allowed the creation of command-line tools including some Popular front-end frameworks which have popular packages dedicated to CLI tools, such as angular/cli , vue/cli or create-react-app.

Node and microservices architectures

Node.js takes full advantage of the modularity of JavaScript by giving developers the ability to create several small services in the form of modules. A way to significantly speed up development while ensuring the scalability of the projects thanks to the single-threadedness of Node.

Increasingly trendy in recent years, microservices architectures in NodeJS are facilitated by the presence of a multitude of packages on npm. Reflections of a solid and growing open-source community, these modules notably make it possible to interface microservices with each other via HTTP APIs or Messaging.

Since each microservice can be deployed, updated, scaled, or restarted independently of the others, project maintenance is greatly facilitated and updates can thus be deployed frequently without affecting the end-user experience.

For which projects is Nodejs not suitable?

While Node has many advantages over more traditional server-side web development methods and languages, it is nevertheless less suitable for certain types of projects.

Node.js is indeed less efficient than other languages ​​such as Python for very CPU-intensive processes or services.

A Node application overloading the processor will have the effect of blocking the execution of all other tasks and consequently slowing down the entire application. Good examples are data science or machine learning involving the analysis of large volumes of data.

More generally, NodeJS is less suitable for projects that do not require the creation of a web server. For all the others, it is a solution of choice that convinces more and more developers.

What are the best tools for Node.js?

The features offered natively by Node are powerful but basic: they are not enough to create a complete application. However, the developer can count on the thousands of modules available but also on essential tools. 

You may like to read our list of top JavaScript Libraries and frameworks.

Best IDEs for Node

A well-configured integrated development environment (IDE) compatible with Node is a mandatory requirement for coding comfortably. While many solutions are available, three software stand out as the best IDEs for NodeJS :

  • Visual Studio Code dominates the JavaScript developer market with over 70% market share. Since its release by Microsoft, this IDE has attracted many followers thanks to its simplicity, its many plugins, and its optimal performance.
  • Webstorm IDE is very complete, offering all the features needed to develop Node applications. However, it is heavier and requires a longer learning and handling time. It is developed by the world’s largest IDE developer, JetBrains, best known for its IntelliJ and PHPStorm solutions.
  • Atom is the IDE developed by GitHub before the takeover by Microsoft. Available for free, this IDE was able to make a name for itself when the only competitors were Sublime Text and Notepad++. Over time, she was able to integrate many plugins to add all the necessary features but at the cost of lower performance. (Atom is no longer maintained. Nevertheless, you may still download it from here).

The best versioning tools for NodeJS

Version management (or versioning) is a fundamental element of IT development since it makes it possible to keep a history of the different versions of the project, compare the versions between them, or facilitate collaborative work between developers on the same project.

Git is the most widely used version control software in the world.

Free software created by Linus Torvald (the creator of the Linux kernel), Git works in a decentralized P2P ( peer to peer ) mode: the project code is stored on each contributor’s computer and can also be stored on a distant server.

Git also allows splitting code into multiple branches (e.g. development, test, and production ) and tracking trees (folders and sub-folders)

In order to use git with NodeJS, several tools exist:

  • GitHub, a hosting with Git versioning. It allows developers to store their code in managed online repositories, easily add contributors with different permissions, and other free and paid features. Acquired in 2018 by Microsoft, GitHub is a true open-source hub and the dominant player in Git hosting. Its main competitors are GitLab and BitBucket.
  • While Git works through the use of terminal commands: git init to initialize a new repository, git clone to copy an existing repository, or git commit to add changes to a repository, GitKraken provides a graphical interface for viewing and performing all these operations. Using such a solution can save considerable time and convenience for developers who are unfamiliar with Git commands, and even others.

The best testing tools for NodeJS

The essential skill to progress in a development career is to know how to test your code. Automated testing ensures that your code remains functional as it grows. With unit and functional testing, you ensure that a newly added feature doesn’t break another element of your application.

To perform these tests, you must add to your code one or more modules capable of browsing your code and evaluating what comes out of it. Each language has its test libraries, and for Node or more generally JavaScript, Jest, and Mocha are the two most popular frameworks.

Best Deployment Tools for NodeJS

Among the technologies that have revolutionized the Internet in recent years, cloud computing is among those that have had the greatest impact on the world of web development. It consists of making computer resources available (most often storage space or computing power) through remote virtual machines rented on demand.

When it comes to deploying NodeJS applications and solutions the cloud generally allows faster deployment and lower infrastructure costs compared to using, for example, a dedicated server.

The three main services offered by cloud computing are:

  • IaaS (Infrastructure as a Service): provides developers with a ready-to-use and scalable infrastructure as needed (storage space, bandwidth, computing power, etc.). Developers only have to manage the software (installation of an operating system, different applications, etc.).
  • PaaS (Platform as a Service): includes the functionalities of IaaS but also provides software resources (operating system, database management, decision support software, etc.)
  • FaaS (Functions as a Service) or Serverless: the service provider is solely responsible for executing a code by automatically allocating the necessary resources to it. Typically, this code is executed within a container triggerable event.

For Node applications deployment, the cloud is widely preferred over more traditional options. Among the main providers of cloud solutions adapted to Node.js projects: Amazon Web Services, Google Cloud, and Microsoft Azure are among the most popular

Node.js and database: which solution to choose?

Databases are fundamental to the vast majority of application development projects and NodeJS is no exception. The choice of a database management system is a crucial decision that will impact all stages of the project, from the preliminary study to maintenance, including development and production.

Whether installed on-premise or via a DBaaS service, NodeJS is compatible with all major database management systems. They can be distinguished according to their ability to manage relationships.

Relational databases and NodeJS

A relational database management system makes it possible to manage databases in which the stored information can be linked together. Databases are separated into different tables that can be likened to arrays: they contain columns (which define what data is present, for example, last name, first name, date of birth ) and rows (which are entries in the database, for example, BONAPARTE, Napoleon, 15/08/1769 ).

Structured Query Language (SQL) is the standard for performing queries on a relational database. It allows you to reconstitute a set of information contained in several different tables in a single request, without having to restructure them.

One of the most widely used relational database management systems in the world is MySQL.

It turns out that the majority of Node projects don’t require strict relationships between data. In this case, the NoSQL solutions are to be preferred.

NodeJS and NoSQL databases

As their name suggests, NoSQL databases work without the SQL language. Beginning in the 2000s, the NoSQL movement was mainly popularized thanks to the technologies developed by GAFA. (Google, Apple, Facebook, Amazon) It was then a question of responding to the problems of big data.

Unlike SQL solutions, most NoSQL solutions do not require a predefined data schema (those famous “columns” which define which data can be inserted but also their type: character string, date, whole or decimal number, etc. .) and are therefore faster to set up, lighter and more flexible. Logically, they do not manage the relationships between the stored data.

There are four main models of NoSQL databases.

The key-value model

or Key – value stores hashmaps has the form of an associative array.

The main NoSQL key-value database systems for NodeJS are  Redis which offers a bit more functionality than just key-value storage, and Memcached which focuses only on key-value but is a bit more capable.

The document-oriented model

Documents equally associate information with a key, but this information can be other documents, which implies hierarchical data management.

The most popular document-oriented NoSQL system used with NodeJS is MongoDB. It can be used natively with the mongo library or with Mongoose to facilitate document modeling.

The column-oriented model

Column-oriented systems store data by column and not by row: the system first serializes all the values ​​of the same column, then those of the next column, etc.

This system is based on the principle that we usually don’t need all the columns in a query, but only some of them. It allows columns to be hosted on different servers and to parallelize queries for optimal performance, especially for large data volumes.

The iconic representative of column-oriented systems for NodeJS is Cassandra, developed by Facebook.

The graph-oriented model

data Graph with databases in the form of a graph, nodes, arcs, and properties. This structure makes it easy and quick to find information hierarchized in a complex way (by going through the links between each element) and whose modeling according to other models would have been much more difficult.

With its driver for Node neo4J-driver, Neo4J is surely the most suitable solution for using a Graph Database with NodeJS.

Express.js: the best Node framework?

If Node.js makes it possible to speed up application development, it is largely because there are many compatible modules. Among these, Express.js is the most popular framework for Node JS.

Written in JavaScript, the Express framework is lightweight, flexible, and simplistic.

Natively offering only a reduced number of packages, Express does not offer a command line interface, suggested architecture, or ORM in order to maintain optimal performance while simplifying the design of a Node server.

Express is designed to leave a lot of freedom to developers, especially in the choice of modules to integrate into the project. For example, where other more rigid frameworks such as Rails (Ruby) or Django (Python) impose the use of a database system (which can be circumvented by replacing the modules provided by default), the Node developer just needs to import the library corresponding to his personal choice: MongoDB, MySQL, Cassandra, Neo4J…

This freedom also brings its share of disadvantages by increasing the risks associated with a lack of consistency in the architecture and the modules chosen. This can negatively impact the maintenance and debugging of a complicated Express application when it was designed with flaws and a lack of documentation.

For more information on building a Node server with Express.js, you can check out our complete guide to Express: All about the Node framework.