If you have been using Ruby, Python, PHP, or Java languages, you may not know how much you are missing out if you are yet to try Node. One of the greatest tasks programmers have to deal with is boilerplate code that churns out using any of the programming languages.

It is not a surprise that many heavyweight companies like Walmart, Paypal, and Capital One run a certain part of their stack only with Node. Node, in those scenarios, is used either as orchestration layer or as an API gateway. Nothing is as rewarding as being fully submerged in the Node environment.

If you live by the adage that says developers should be lazy, like Samuel Overloop, Node.js will get you lazier. This is because you can use it to write cleaner - and less - code both in unit testing and in application code.

If anyone advised you to use ES2015, they will agree that a lot has changed, and you will need some upgrades too. The latest version was Node.js V4. During its release, Node.js enjoyed the support of just about 57% of ES2015 functionalities. In about a year, ES2015 support has spiked to 99%, following the release of Node v6. If you want to know more about which ES2015 features are supported by the different Node.js versions, the best place to begin your search is here.

If you are new to programming, this post may not be much help to you. You may need to take a look at online resources that go down to the basics of programming. But if you are already a programmer, the following tips on Node.js will make you even better.

1. De-clutter your codes

Your target, as a programmer, should be how to make your codes smaller. The creator of npm, Isaac Z. Schlueter, says, “It is worth emulating when it comes to coding.” For example, JavaScript strict mode for modules is enforced by use-strict which is only three lines long. So, why leave it at 'complex' when it can be made simpler? Modularizing your codes into smaller parts makes it better understood by other developers - and this is crucial if you are working with a team. Keeping codes simple also makes their testing process easier.

A useful tip by Azat Mardan in a guest post to Sitepoint is to code for the dumber version of you. Mardan advises that if you write codes at the summit of your mental capability, it may be hard for you to interpret much later when you are not operating at your peak.

2. Don’t block “require”

Node module loading system uses the CommonJS module format. Existing modules can be included in separate folders, using the built-in “require” function. The working technique of ‘require’ is to import what was exported in a file or module. The loading of Node/CommonJS is synchronous, unlike the AMD/ requires.

The ‘require’ is cached - unknown to many developers. This means that if a resolved filename is not drastically altered, the codes contained in the module, for that process, gets loaded and executed into the variable. When the module import occurs at the point of request, it makes the /connect route slower. A better way of dealing with this is loading the modules long before defining the server.

3. Keep watch over your application

You should be the first to spot an error in your Node.js application. Prometheus is an open-source, newer solution you can employ to safeguard you. Originally built at SoundCloud, Prometheus has a toolkit for monitoring and alerting. Another tool that is of great help is RisingStack’s Trace. Trace performs an array of functions, including alerting, performance monitoring, memory and CPU profiling in production systems, keeping npm packages secure, and distributed tracing and error searching.

4. Practice asynchronous code

There is a small place for synchronous code in Node. The synchronous code comes into Node when CLI commands or other scripts are to be written, but nothing related to a web app. To avoid blocking threads, Node developers employ asynchronous code during building the web apps. The major difference lies on whether your writing systems are concurrent, or non-concurrent.

5. Double check for errors

Java is significantly different from Node. When you are working with Java, you wouldn’t mind a few errors which could be resolved later at a higher level, using a single try catch. When you are running Node asynchronously, it makes use of the event loop. This splits errors from the framework of any error handlers. However, you can still perform try catch on Node codes that are synchronous.

Always handle errors in Node manually. A node developer presents errors as call back argument and every call back argument must be tackled singly, and manually.

6. Automate everything

If a process is automation friendly, you don’t need to waste your time on it. A lot of tools have evolved around JavaScript, including Gulp, Grunt, and Webpack. Many developers have found Webpack rewarding for front-end development. Webpack can be a hard nut to crack for newcomers, but you can get help from Webpack Cookbooks.

7. Deal with bugs using return statement

Node features can easily turn into bugs if they are not handled carefully. This is because Node is concurrent. As a Node developer, the safest way to avoid this is to use a return statement to terminate their execution. You should always avoid the unintended concurrency that may arise from mishandled control flow. Always - return ‘a callback to stop the continuation of its execution.

8. Have a good knowledge of your npm

Many front-end and Node developers are well aware of “save”, which creates an entry in package.json and also installs a module. There is also save-dev for dev Dependencies. What if you are told you can use S and D instead of save-dev and save? Also, in the module installation mode, take out the ^ symbol created by S and D. If they still remain, they will allow npm install.

The team at npm believes in semver, but you shouldn’t. The team believes that open source developers would not introduce significant changes to minor releases. This is why they introduced the caret.

9. Pay attention to error events

Nearly, all Node objects/classes lengthen the observed pattern and discharge the error event. This presents an opportunity for a developer to catch the irksome errors and handle them before they cause any serious problem. You can inculcate the habit of creating event listeners for errors using .on().

10. Employ semantic versioning (semver)

It is sad that only about 71% of developers use semantic versioning when consuming/publishing modules. This was revealed in the Node.js Developer Survey. The main reason you should use semver is that when you update packages without them, Node.js apps can break easily.

11. Keep your applications secure

Your top priority is the security of your customers and users data. Hundreds of user accounts were compromised in 2016 due to low-security level, and you don’t want that to happen to you. A resource you will find invaluable to nullify the security threat is “Surviving Web Security with Node.js.”

Many basic tips you need to become a better Node developer have already been covered. In addition, you can learn a great deal about new trends when you attend conferences. If you are bold enough to speak at one of the conferences, speaking.io can help you with a few public speaking tips. Bidding for a Node job on Freelancer will provide you with the opportunity to put all you have learned into practice, while also helping you earn yourself a potentially fat living.

Did you find this useful, or you think there is an important point still missing? Drop your opinion in the comment box below.


Souscrivez maintenant pour des mises à jour hebdomadaires

Restez à jour avec les meilleurs articles de la communauté Freelancer. Souscrivez maintenant pour le meilleur contenu de Design, de Développement et de Freelancer chaque semaine.

Publié 1 septembre, 2017


Software Developer

Lucy is the Development & Programming Correspondent for Freelancer.com. She is currently based in Sydney.

Article suivant

Aurelia vs. Angular 2 — A Code Comparison