Node.js and JavaScript for server development

Original author: Mathieu Dionne
  • Transfer
In 2009, the Node.js platform took its modest first steps in the vast world of backend development. This was the first attempt to use JavaScript in server applications. Today it will be extremely difficult to find a web developer who has not heard of Node. But you can’t say that the existence of Node was cloudless. This platform survived the split of the community, was the subject of forum wars and drove many to despair. Perhaps you think that such statements sound too pompous. However, try searching on Google and you will come across an inexhaustible source of endless debate.
image

. Among the arguments that are not in favor of Node that you may encounter, there are, for example, those that, asking about what happened to the axiom about using the best available tools to solve a certain problem, indicate that JS was not standing near the right server toolkit. Other criticisms of JS, such as Callback hell is real, calling to believe the reality of hell callbacks, sound like lines from a poem. Some of Node's critics are more direct and unambiguous: "Node is a cancerous tumor."

I believe that the time has come to restore the true state of things, to put all the dots on the “i” with regard to the Node.js platform and JavaScript as the server development language. Today we will talk about the current state and development of Node.js, about the most successful options for using this platform, about its limitations, and about the technologies created on its basis.

Current state of Node.js as a server platform


Before talking about what the Node server platform looks like today , let's recall what it is.

Namely, it is a JavaScript runtime built on the basis of the V8 JS engine developed by Google and used in Google Chrome. Node.js uses a non-blocking event-driven I / O model that makes this platform simple and efficient.

At the beginning of this article, Node is shown as a programmer’s nightmare. However, this platform did not accidentally become very popular. Here we will not rely on unfounded allegations. Better take a look at the facts. Namely, a recent study by Stack Overflow shows that Node.js is, by far, the most popular technology among developers.


In addition, JS is a language whose popularity has grown faster over the past five years than other languages, while C # and PHP are losing ground. The prevalence of JavaScript, not even talking exclusively about Node, goes up.


How can we explain the fact that JavaScript, in the role of a server language, was so quickly and widely accepted by the development community? Simply put, Node went through a stage in which it was perceived as some kind of fun, and entered a phase of stability and maturity. A powerful community has formed around him, the size of which is growing steadily. The Node ecosystem is also worth mentioning, as, for example, the Node package manager, npm , is currently represented by the largest software registry on the Internet.

Node.js not only made a revolution in server development, but thanks to it, a contribution was also made to the performance of client applications, since serious forces were involved in the development of V8. In addition, it plays a prominent role in expanding the entire JavaScript ecosystem and in improving modern JS frameworks such as Angular , React, or Vue .

Over time, Node was able to refute the prejudices of the early days. Here is some of them.

JavaScript code for Node.js is notorious for debugging complexity.

For debugging server-side JS applications, you can use the same methods that are used to debug client code using node-inspectorwhere the Chrome Developer Tools tools are compiled.

Further, for example, developers from Joyent, who understand something about debugging and profiling Node applications, have long released the universal DTrace debugger .

Node cannot be used to develop enterprise-class server applications.

This statement is also not true. Based on Node, you can create enterprise systems. The only difficulty is that it does not have many built-in tools that simplify the creation of such systems. However, notable IT market players use Node as their corporate web platform. Among them is Netflix. PayPal, Yahoo !, Walmart.

JavaScript is a dynamic language, therefore, working on it, you can not use something like static type checking during compilation.

It's true. However, tools like TypeScript and Flow appeared in the JS ecosystem, which are aimed at working with types in JS, which helps to increase the stability and predictability of programs, and simplify debugging. In this area, you can take advantage of the Closure Compiler from Google.

JavaScript was not created as a language for server development.

Here we can only say that JS could already work on servers , at the same time that Netscape built support for this language in its browser. And it was already in 1995. JS is usually called the language of client web development only because it completely captures this area.

In fact, the list goes on and on.

Now let's talk about the scenarios for using Node.js and its limitations in order to better understand the place of this technology in the modern world.

Node Application Scenarios


So, why even consider Node.js as a server development tool in your technology stack?

▍ Advantages and General Features


Let me briefly outline the most important:

  • It is very likely that the client parts of your web applications are written in JavaScript. In this case, the universality of the code in the applied technology stack is an important plus of using JS on the server, which is worth remembering.
  • Tools like webpack help in reusing code on both the client and server, which leads to its uniformity at all levels of the system.
  • Using JS on the client and on the server , you can create web applications that can be rendered both in the browser and on the server . Moreover, such systems usually work very clearly and clearly. I guess it's just awesome.
  • The appearance of the async / await construct in Node completely changed the approach to writing asynchronous code. Now such a code resembles a regular synchronous code, both in appearance and in behavior. The async / await mechanism has been supported in Node since version 7.6 . It, in particular, is the solution to the infamous callback hell problem .

Some people see the code base of the client and the server minus Node.js, saying that it forces the developer to use JavaScript. However, this is not quite true. If necessary, you can access third-party specialized libraries from Node applications.

Let's say you need video encoding tools. In order to equip your project with them in JavaScript, you do not have to look for some rare mysterious libraries for Node. You will be able to fully use the proven tools, having established interaction with them from Node. Or, for example, if there is a certain Python library that performs the complex calculations you need, you can run a microservice specifically for working with it and access the corresponding functions of this library through the REST API.

Given all of the above, the following options for using Node.js can be distinguished, in which it fully reveals its strengths.

▍ Scenario number 1. Real time applications


Collaboration applications (such as Trello and Google Docs), interactive chats, instant messaging systems, and online games are examples of real-time applications that can help you with the development of Node.js architecture.

The time required for these applications to perform certain actions can be characterized, from the point of view of the user, by the words “immediately” and “now” . For the normal operation of such applications, the system on which they are based must provide a very high response speed to user actions and a low level of delays. Node meets these requirements.

Node facilitates the processing of many requests from clients, its architecture promotes the efficient use of libraries, it provides very fast data synchronization between the client and server.

▍ Scenario number 2. Single Page Applications


A single-page application is an application that is represented by a single web page loaded into the browser, the contents of which are dynamically updated as the user interacts with it. Most of the workload of such applications falls on the client side of the system, written in JavaScript.

Even though single-page applications are a significant step in the evolution of web development, they also have problems that, for example, relate to rendering. In particular, this can adversely affect search engine optimization . A popular solution to these problems is server-side rendering using Node.js.

▍ Scenario number 3. Scalability


A server on Node will never be much more powerful than necessary. The beauty of Node architecture lies in its minimalism, in that the server side of applications can be scaled depending on the needs of the project. The secret here lies in the right attitude to performance.

Even the name of the subject of our conversation, “node” focuses on the possibility of building systems from many small distributed computing nodes that can exchange data with each other.

The modularity of Node allows you to create small applications without encountering the need to support a huge infrastructure, many parts of which in some specific case will be unused. When developing Node applications, the programmer chooses exactly what he needs and, if necessary, extends the solution.

However, it should be noted that certain difficulties are associated with such scaling capabilities. And, if you lose vigilance, Node.js can become ... dangerous.

Limitations of Node.js


To be honest, Node allows the developer, as they say, “to shoot yourself in the foot”. In this world you have to pay for everything, including - and for the wide possibilities for setting up the system and for adapting it to your needs. If you work with Node without having enough experience or regularly letting things go by chance - you can encounter serious problems such as losing customers.

In contrast to the more traditional approaches, which strictly regulate certain architectural features of systems, the structure that supports the server part of your application is created by you. As a result, there are many decisions to be made, that is, the developer needs to know exactly what he is doing and what will happen to the project if it needs to be expanded.

In the case of other languages, such as Ruby, and the well-known Ruby on Rails framework, for example, the idea that lies in the advantage of conventions over system configuration is in use. These traditional frameworks literally lead the developer by the hand, showing him the right, safe way to solve typical tasks.

Node turns everything, so to speak, upside down. The developer is given more freedom, but the path to the implementation of the plan can be full of dangers if the wrong decisions are made during the work. Here it will be appropriate to recall the notorious “callback hell”, which suddenly turns out to be completely real.


This does not mean that you can not create large server applications on Node, but the above should always be kept in mind.

Even the creator of Node.js, Ryan Dahl , in the end, before moving on to other projects, realized the limitations of the system. He spoke on this subject very clearly:

I think Node is not the best system for creating large-scale server projects. I would use GO for this . And, frankly, that's why I quit working on Node. "I once realized that, in fact, this is not the best system for server development ."

The previously mentioned prejudices regarding Node were valid until a certain point in the not-so-long life of Node, and they, to some extent, are still not an empty phrase. Node has matured and grown enough, its weaknesses, if necessary and the availability of time, can be easily circumvented. And the tools developed by the community allow you to create almost anything on the basis of Node.js.

Popular JS Server Development Tools


Not too long ago, if someone thought about creating all the parts of their system on JS, the idea of ​​the MEAN stack (MongoDB, Express, Angular and Node) immediately came to my mind .

This set of tools has not lost its relevance these days, however, at present there are much more interesting tools in the JS ecosystem for both client and server development, so do not limit yourself to MEAN.

Here are some popular modern server-side JS frameworks:

  • Express.js was and still is the most popular Node.js framework . It is fast, compact, does not impose hard architectural solutions on the developer. Its rapid development is based on simplicity and comprehensibility. Perhaps it is ideologically closer than all other tools to the underlying ideas, Node, following which it is a lightweight modular system.
  • Meteor , on the other hand, uses pure JavaScript and Node.js inside a rather large-scale construction. Meteor itself is an entire ecosystem that may be suitable for developing more complex server applications. However, using Meteor can be complicated if you need something that is not built into the system.
  • Sails.js is a real-time MVC framework. It was designed to simulate the MVC pattern on the Ruby on Rails platform, but at the same time implied support for the requirements of modern web applications. It all works thanks to data-driven APIs with a scalable, service-oriented architecture.
  • Koa.js is a framework created by the same team that deals with Express. It is promoted as the “Next Generation Framework for Node.js.” It can be described as a system that is compact, expressive and reliable. Koa.js is suitable for developing web applications and APIs.

Of course, this short list does not contain everything that has been created for server development based on Node. Here are some more interesting projects: Nest.js , Hapi.js , Socket.io , Mean.js , Total.js , Derby.js and Keystone.js .

Summary


The purpose of this article is not to make a final conclusion about whether the Node.js ecosystem offers the best tools for server development. It is not a question of inducing anyone to use Node. In addition, I'm not going to say that Node is significantly superior to other popular server environments that are developed using Java, C #, C ++, PHP, Python, Go or Ruby.

I suppose the most important thing I wanted to do was to show that between the categorical “yes” and “no”, which can be found in many discussions about Node and about JS as a server language, if the vast area “can be”.

And whether someone likes it or not, interest in Node is constantly growing.


To conclude this material, I would like to say that you should not treat any framework as a magic wand that will magically solve all problems. Node.js is just one of the tools in the colossal web development universe. In some situations, he solves the tasks assigned to him exceptionally well, and in others, working with him can turn into a nightmare.

The tasks of each developer include a careful selection of technologies suitable for the implementation of any new project. At the same time, it is important to take into account all the possibilities and not immediately discard the available alternatives.

For example, the company Snipcart, where I work, uses the .NET architecture, which you can talk about a lot of unflattering. Why is she chosen? Yes, simply because at a particular moment it turned out to be the best tool for solving the task set before us.

I hope my story about Node will help you make the right decision when choosing a server platform for your next project.

Dear readers! What do you think of server-side JavaScript and Node.js? Can you share examples of successful (or unsuccessful) use of Node in real projects?

Also popular now: