6 common mistakes developers make in Node JS
- May 12, 2017
- Posted by: admin
- Category: Node.js, Web App Development
As with every platform and programming language, Node JS has its fair share of critique and praise. Typically, the criticism revolves around safe code writing practises and how it is complicated in Node and so on. Having been into Node.js development for a long while now, we have come with some of the common Node JS mistakes that developers should avoid; read on to learn more.
1. Call backs being invoked multiple times
This is surprisingly a very common mistake developers make and when someone else makes use of the code, they might be in for a huge surprise! After all, a call back isn’t the end point of the current function and some developers have the practise of adding a return before the call back to avoid multiple invocations. In most functions that are asynchronous in nature, the return statement is practically insignificant and thus helps in avoiding this problem. We recommend that you keep this in mind while working on Node.js development.
2. Call backs don’t run synchronously but most developers expect them to!
3. Error throws from call backs
Looks like most of the trouble brews with call backs, huh? Again the problem here is with asynchronous behaviour. Your try and catch may not behave in the manner that you wish it to. This is why most Node.Js developers actually follow the (err,..) pattern for the arguments of the call back function wherein the error is the first argument of the call back.
4. Numbers are floating data types in JS
5. Using Console.log to debug instead of the debug library
We can understand that it is indeed tempting to print out everything using console.log and later comment it out. We recommend that you make use of the Debug library for debugging as it meant for just that very purpose and also you can prevent logs from being printed by simply turning off the debug mode thus ensuring safety and preventing leaks.
6. Handling the event loop
Typically concurrency is achieved in Node by running Input Output Operations in an asynchronous fashion. If you wish to perform some operation that is CPU intensive with a front facing Node that clients use concurrently, you are in for trouble. Look for better solutions to handle this and avoid blocking the event loop.
Some of the other common mistakes that we find in usual Node.Js development are: not streaming APIs, not using Supervisor programs such as nodemon, supervisor and so on, assignment to exports instead of the module.exports and understanding the difference between them and finally call backs that are deeply nestled. Avoiding all of these could help you write better code; do try these out!