Table of Contents
Instead of the traditional web-serving technique, where each request creates a new thread in the system RAM, Node.js uses a single thread. This allows it to handle hundreds of connections at once. Node.js also offers package management support using npm. The popular package library, npm, is one of the greatest conveniences for Node.js developers. There are millions of downloadable libraries for a specific need. Another feature of these great libraries is that they are free.
What Is Node.js Used For?
Node.js offers developers an unlimited environment. Today, there are many platforms where Node.js is used very successfully. These include social media networks, single-page applications, chat applications, data streaming, and IoT applications.
Most of the applications developed with Node.js are based on REST API development. In this article, common mistakes made by Node.js developers while developing Node.js will be discussed.
Common mistakes while developing in Node.js
Profiling and tracking
Profiling information aids program optimization by examining various aspects of the program, such as space or function turnaround time. Profiling in Node.js applications helps you understand things like event loop latency, system load and CPU load or memory usage.
Translation results Callback was the first return method used in asynchronous functions. It is given as a function parameter and the callback function is called after operations are performed in the asynchronous function
Callback Hell, on the other hand, turns your code into a structure with intertwined Callback stacks in cases where these asynchronous works are too much, one after the other, or when there is a need to work together. It’s called Callback Hell. You can see this situation in the example below. You can see that the code is becoming increasingly unreadable.
Invoking a callback more than once
In Node.js, the only way for asynchronous elements of your code to communicate with each other until promises are made is to use callbacks. Callbacks are still used and package developers still design their APIs around callbacks. A common Node.js issue with using a callback is calling them more than once. Typically, a function provided by a package to do something asynchronously is designed to wait for a function as its last argument, which is called when the asynchronous task completes.
Using to “exports”, Instead of “module.exports”
If you are using Node.js, a scenario like the one below will look familiar to you.
Above; We can access the title property in ‘book.js’ thanks to exports. But in this approach we always need to call a method or property. So how do we pass our own object to the exports property?
In such a case, we use the ‘module.exports’ feature.
Above; We did not call any method or property, we reached the book variable directly and got the return as we wanted. In some cases, you may have to use this method.
Note how we treat “exports” as a property of the module object and use it in your projects. The difference between “module.exports” and “exports” here and the usage scenarios coming from this difference are quite important.