In the last few articles, we’ve established that Node.js is a great language to work with, whether you are developing consumer or enterprise applications. Despite all the advantages of it, Node.js still receives its fair share of bashing, which is mostly because of some common mistakes that many developers make when developing apps with Node.js. Some of the most aggrandized Node.js problems like Callback Hell and slow NPM installs, can all be avoided, if you carefully avoid mistakes and use the right tools.
When left unchecked, it is these mistakes and not Node.js itself that create difficulties in Node.js development, earning it an unfair bad rap.
1. Executing Multiple Callbacks
Simply keeping an eye out for such an error is a good first step. You can add a return before the callback to avoid invoking the callback multiple times. The return statement has no significance in a majority of the asynchronous functions so you won’t have an issue. You can also try using an else branch, sometimes even if it isn’t necessary, to avoid invoking the same callback twice. Another great option is to wrap the callback so you see an error anytime it is called twice.
2. Callback Hell
This one just happens to be the favorite argument of Node bashers. The developers too complacent and unwilling to try anything new will present callback hell as the supreme example of Node.js’ shortcomings, telling you that nested callbacks are just unavoidable. That however, couldn’t be farther from the truth.
Callback hell is what happens when multiple asynchronous operations pile on after each other. If you follow the previous point and avoid overloading your functions in the first place, you can prevent callback hell to begin with.
There are some really useful tools that can help you avoid callback nesting and keep your code clean and tidy. Three of the top tools to help avoid callback hell are:
- Control flow modules like Async
Promises were created to solve the problem of callback overload in Node.js. If you aren’t using it yet, start now. Promises provide control over the value of the result or an error exception. The core function of promises is the .then() function. .then() waits for the promise object to be returned and takes two optional functions as arguments and calls only one, depending on the state of the functions. The first function is the one called when the promise is fulfilled; the second one is called when the promise is rejected. This way, Promises help create cleaner code.
Generator is different from Promises that works with Promises though. Generators execute asynchronous events without blocking the code. In fact, they make your code look like synchronous code.
3. Blocking the Event Loop
Developers just have to come to terms with the fact that Node.js is single-threaded. So anything that blocks the event loop will block everything. So no two parts of your application can run parallel. Just injecting a piece of CPU-bound code when Node.js is busy fetching a document from the data base system is enough to block an event loop. So it is important that developers address each case individually and in general, stay away from CPU intensive tasks within the front facing Node.js instances. You could also use StrongOps or other open-source modules to detect any delays in the loop.
4. Using Console.log for Debugging
To put it simply, don’t use console.log for debugging. Use Debug Library. Allow me to explain.
In Node.js, the console.log will print absolutely anything literal. Even if it is just an arbitrary argument, console.log will print it, all along keeping it typed and spaced well. This makes developers feel extremely inclined to debug everything with the console.log. But the problem here is, each time you insert the console.log, you need to restart the server and as a result, you end up slowing down the app. in the end, you will have a messy, unclean code as well as a ton of unnecessary code. To make matters worse, the next developer taking on this project might just repeat the entire process.
Avoid all that mess simply by using the Debug module. Instead of having to plug, restart and delete console.log multiple times, just use the Debug library.
5. Taking Numbers for Integer Data Types
So those are the five major mistakes developers commonly make when developing with Node.js. Node is indeed a great language to develop apps in, with a number of modern features designed to make app development faster, easier and safer. As long as you understand these intricacies of Node.js and consciously avoid the aforementioned mistakes, you will be a happy, content and much more productive Node.js developer.