{"id":95255,"date":"2020-10-22T17:00:15","date_gmt":"2020-10-22T14:00:15","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/here-are-10-best-practices-for-node-developers\/"},"modified":"2020-10-22T17:00:15","modified_gmt":"2020-10-22T14:00:15","slug":"here-are-10-best-practices-for-node-developers","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/","title":{"rendered":"#Here are 10 best practices for Node developers"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2598f6ea387\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2598f6ea387\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Save_the_exact_package_version_to_packagejson\" >Save the exact package version to package.json<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Use_a_style_guide\" >Use a style guide<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Use_asyncawait_in_place_of_callbacks\" >Use async\/await in place of callbacks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Always_try_to_use_%E2%80%98const_over_%E2%80%98let\" >Always try to use \u2018const\u2019 over \u2018let\u2019<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Use_the_operator_over_operator\" >Use the === operator over == operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Dont_serve_your_frontend_assets_from_Node\" >Don\u2019t serve your frontend assets from Node<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Keep_your_application_stateless\" >Keep your application stateless<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/here-are-10-best-practices-for-node-developers\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p>&#8220;<strong>#Here are 10 best practices for Node developers<\/strong>&#8221;<\/p>\n<div>\n                            Node.js is popular among developers as an asynchronous and event-driven language used for backend development. It has a syntax easy enough to understand even for beginners.<\/p>\n<p>Whether you\u2019re a beginner or a seasoned developer using Node, following the best practices of Node.js programming is a must when developing <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>lications aimed for production.<\/p>\n<p>In this post, we\u2019re going to point out 10 best practices every Node developer should follow. On top of the <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> best coding practices you should be following as a programmer, these will help you get the maximum use out of Node\u2019s unique features.<\/p>\n<p>[Read:\u00a0<em>What audience intelligence data tells us about the 2020 US presidential election<\/em>]<\/p>\n<h2 id=\"save-the-exact-package-version-to-packagejson\"><span class=\"ez-toc-section\" id=\"Save_the_exact_package_version_to_packagejson\"><\/span>Save the exact package version to package.json<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>During development, we\u00a0 need to add a new package to the application using the following command:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324721 lazy\" alt=\"\" width=\"683\" height=\"89\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.07-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.07-PM.png 683w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.07-PM-280x36.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.07-PM-540x70.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.07-PM-270x35.png 270w\"\/><\/figure>\n<p><\/span><\/p>\n<p>It saves the package with its installed version to the package.json file like this:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324722 lazy\" alt=\"\" width=\"685\" height=\"84\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.11-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.11-PM.png 685w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.11-PM-280x34.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.11-PM-540x66.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.11-PM-270x33.png 270w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Here, the use of caret sign (^) indicates that a package with a version within the region from \u201c&gt;=4.17.1 &lt;5.0.0\u201d is accepted by the application. (If you want to know more about how symbols like caret define version ranges, read the\u00a0<a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/semver.org\/\">SemVar documentation<\/a>).<\/p>\n<p>This becomes problematic when someone else uses the package.json file we created to set up the application environment and install the dependencies (using the command npm install).<\/p>\n<p>In this case, npm doesn\u2019t install the version of the package we used for development, but instead, the latest version available at the time of the other user installed the dependencies. If the latest version is not compatible with the version we used in the initial implementation, it results in an error. Especially, if the project is open source, so we need to be mindful of this possibility.<\/p>\n<p>We can use two methods to prevent this clash of package versions.<\/p>\n<p>One method is using\u00a0<code>--save-exact<\/code>\u00a0every time you\u2019re installing and saving the package to the package.json file. It looks like this:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324723 lazy\" alt=\"\" width=\"686\" height=\"83\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.34-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.34-PM.png 686w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.34-PM-280x34.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.34-PM-540x65.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.34-PM-270x33.png 270w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Or configure the npmrc file once using the following command so that, by default, the accepted version of every package installed is its exact version. It looks like this:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324724 lazy\" alt=\"\" width=\"687\" height=\"82\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.49-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.49-PM.png 687w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.49-PM-280x33.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.49-PM-540x64.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.18.49-PM-270x32.png 270w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Both these methods save every new package installed to the package.json file without the preceding caret.<\/p>\n<pre><code>\"express\": \"4.17.1\",&#13;\n<\/code><\/pre>\n<p>Now, every time someone else uses the package.json file to install dependencies, npm will install the exact version mentioned in the file instead of the latest version.<\/p>\n<p>While still in the developing stage, you\u2019ll have to stop and restart your application to apply even a simple change made to the code base if you\u2019re not using additional tools. After coding for a while, this becomes a hassle preventing you from completely focusing on the given\u00a0task.<\/p>\n<p>As a solution, you can use a tool that monitors the application\u2019s codebase and restarts the app automatically every time a change is made.<\/p>\n<p>Some of the popular code monitoring packages available for Node are:<\/p>\n<ul>\n<li>Nodemon: Nodemon automatically restarts the application whenever a new change is added to the code. You can initialize Nodemon by replacing node by nodemon on the command.-line. For example, nodemon app instead of node app.<\/li>\n<li>Forever: Forever also provides the automatic restarting feature Nodemon provides, but with additional configuration options. These options include setting a working directory and writing logs that would normally be printed on stdout to a file.<\/li>\n<li>PM2: PM2 is another process management tool you can use. It allows more control and features to manage processes running in production compared to the other two.<\/li>\n<\/ul>\n<p>You can use these tools, especially Forever and PM2, even in a production environment. It will guarantee the application recovers without an issue after a runtime error. It will also ensure the application restarts as quickly as possible in case the server it\u2019s running on goes down.<\/p>\n<h2 id=\"use-a-style-guide\"><span class=\"ez-toc-section\" id=\"Use_a_style_guide\"><\/span>Use a style guide<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When various\u00a0developers are working on the same project at the same time, inevitably, there are differences in the styles of their codes. Because, unfortunately, programmers are a very opinionated bunch.<\/p>\n<p>If your style of code is different from the previous developer who worked on the project you\u2019re currently working on, you will most likely be tempted to reformat the position of braces or replace spaces with tabs to your liking. And you\u2019ll end up spending hours on this mundane, useless task.<\/p>\n<p>Though programmers love to argue about what is the best style for Node programming in our free time, that choice is mostly subjective. So, for the sake of your peace of mind and productivity, I suggest you choose and agree on a particular style guide before the start of a project. You can even find an existing style guide used by other programmers.<\/p>\n<p>Here are a few style guides used by popular tech companies:<\/p>\n<p>There are tools we can use to ensure the rules of the selected style guide are enforced throughout the program. We can use ESLint to validate the written code and Prettier for automatic formatting purposes.<\/p>\n<h2 id=\"use-asyncawait-in-place-of-callbacks\"><span class=\"ez-toc-section\" id=\"Use_asyncawait_in_place_of_callbacks\"><\/span>Use async\/await in place of callbacks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When Node was first introduced, its asynchronous nature was maintained by the use of callbacks. But as any Node developer would know from experience, it doesn\u2019t take long for callbacks to spiral out of control when they\u2019re nested one after the other. This is what we call \u2018callback hell.\u2019 At this point, your code will be quite unreadable.<\/p>\n<p>However, with the introduction of async\/await in ES6 (and promises before that), Node has provided a solution to this problem. So, as a Node developer, it\u2019s your duty to make use of this new introduction instead of falling back into a callback hell when there are ways to avoid it.<\/p>\n<p>This is a mild version of a callback hell:<\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324725 lazy\" alt=\"\" width=\"694\" height=\"391\" sizes=\"auto, (max-width: 694px) 100vw, 694px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.09-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.09-PM.png 694w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.09-PM-280x158.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.09-PM-479x270.png 479w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.09-PM-240x135.png 240w\"\/><\/figure>\n<p>We can easily avoid running into a scenario like this using async\/await:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324726 lazy\" alt=\"\" width=\"689\" height=\"314\" sizes=\"auto, (max-width: 689px) 100vw, 689px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.28-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.28-PM.png 689w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.28-PM-280x128.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.28-PM-540x246.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.28-PM-270x123.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.28-PM-687x314.png 687w\"\/><\/figure>\n<p><\/span><\/p>\n<p>As a best practice in Node programming, always try to replace the callback functions with async and await.<\/p>\n<p>First off, console.log is not a proper logging tool. Forget about using it for your application\u2019s every logging purpose. Among many limitations, console.log doesn\u2019t provide enough configuration options for logging including an option to filter them as required.<\/p>\n<p>Node has several dedicated logging frameworks you can use in your application. Winston, Bunyan, and Pino are a few examples of such tools. You can test and select a one that suits your application\u2019s needs.<\/p>\n<p>In addition to simply logging, they give you the option to define logging levels like error, warning, info, and debug. With a dedicated logging tool, you can also turn on only the logs of necessary levels according to the situation even when the application is in production.<\/p>\n<p>Don\u2019t forget to follow the\u00a0<a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/2020-09-14-you-built-your-node-app-but-are-you-logging\/\">best logging practices<\/a>\u00a0when creating a logging system using your selected tool.<\/p>\n<h2 id=\"always-try-to-use-const-over-let\"><span class=\"ez-toc-section\" id=\"Always_try_to_use_%E2%80%98const_over_%E2%80%98let\"><\/span>Always try to use \u2018const\u2019 over \u2018let\u2019<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use const to declare a variable over let whenever possible.<\/p>\n<p>When you use const to declare a variable, you can\u2019t reassign a value to it after the first assignment. This prevents you from reusing the same variable on several occasions. Since const forces you to declare new variables with appropriate names without reusing existing variables, it makes your code cleaner and clearer.<\/p>\n<p>But there are occasions you may want the flexibility of being able to change the variable value. Then, you can use \u2018let\u2019 to declare the variable, but only if you have properly identified it as a variable that changes its value. For example, you have to declare the incrementing variable inside a for loop using the let keyword.<\/p>\n<p>Before let was introduced in ES6, var was used in Node to declare variables. Some developers still use var in their code out of habit. But as a Node best practice, don\u2019t use var in your code any longer.<\/p>\n<p>It\u2019s also important to be mindful of the\u00a0<a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/2020-07-25-understanding-variables-scope-and-hoisting-in-javascript\/\">scopes of these keywords<\/a>\u00a0, whether it\u2019s const or let, before using it to declare a variable.<\/p>\n<p>We didn\u2019t list\u00a0<a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/2020-08-17-testing-in-nodejs-using-mocha-and-chai-part-1\/\">testing your Node application<\/a>\u00a0as one of the best practices because it\u2019s something that goes without saying. Using a code coverage tool, however, is one of the best practices you should definitely follow as a Node developer.<\/p>\n<p>We can use a code coverage tool to determine the level of test coverage in the program. Such a tool identifies if there is a decrease in testing coverage and highlights testing mismatches.<\/p>\n<p><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/github.com\/istanbuljs\/nyc\">Istanbul\/NYC<\/a>\u00a0is a great tool you can use for this purpose. It provides colored code coverage reports to help you easily identify areas that are not covered by unit tests you have written. You can easily set up NYC to work with your preferred testing framework.<\/p>\n<p>First, install the package from npm as a development dependency:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324727 lazy\" alt=\"\" width=\"687\" height=\"88\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.45-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.45-PM.png 687w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.45-PM-280x36.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.45-PM-540x69.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.45-PM-270x35.png 270w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Then, add the script to run NYC and generate the code coverage report:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324728 lazy\" alt=\"\" width=\"689\" height=\"187\" sizes=\"auto, (max-width: 689px) 100vw, 689px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.58-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.58-PM.png 689w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.58-PM-280x76.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.58-PM-540x147.png 540w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.58-PM-270x73.png 270w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.19.58-PM-687x187.png 687w\"\/><\/figure>\n<p><\/span><\/p>\n<p>Now you can use the npm coverage command to generate the report.<\/p>\n<p>Read the documentation I have linked above to understand how you can configure NYC to suit your application.<\/p>\n<h2 id=\"use-the--operator-over--operator\"><span class=\"ez-toc-section\" id=\"Use_the_operator_over_operator\"><\/span>Use the === operator over == operator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Node, you can use both === operator and == operator to check the equality of two variables. While the first one is more exacting, the second one accepts weaker equalities as well.<\/p>\n<p>With the === operator, both variables must be of the same type in addition to being similar in value:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324729 lazy\" alt=\"\" width=\"695\" height=\"349\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.11-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.11-PM.png 695w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.11-PM-280x141.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.11-PM-538x270.png 538w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.11-PM-270x135.png 270w\"\/><\/figure>\n<p><\/span><\/p>\n<p>The second operator, ==, accepts two variables as equal if their values are equal even when the types are different. It converts both variables to a common type (type coercion) before carrying out the comparison:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, monospace;\"><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1324730 lazy\" alt=\"\" width=\"692\" height=\"349\" sizes=\"auto, (max-width: 692px) 100vw, 692px\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.24-PM.png\" data-lazy=\"true\" srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.24-PM.png 692w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.24-PM-280x141.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.24-PM-535x270.png 535w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/Screen-Shot-2020-10-21-at-3.20.24-PM-268x135.png 268w\"\/><\/figure>\n<p><\/span><\/p>\n<p>As you saw in the above examples, using the == operator gives different answers than what we may normally expect. This is because Javascript behaves against our intuition during type coercion. It won\u2019t be a problem if you can remember all the odd cases and stay mindful of them when writing the program using the == operator.<\/p>\n<p>But you can easily save worrying about this unintuitive behavior by using the strict equality operator === for comparison.<\/p>\n<h2 id=\"dont-serve-your-frontend-assets-from-node\"><span class=\"ez-toc-section\" id=\"Dont_serve_your_frontend_assets_from_Node\"><\/span>Don\u2019t serve your frontend assets from Node<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node, by its nature, uses only a single thread to serve all the requests made to the server. We have to be mindful about maximizing the efficiency of this single thread because, otherwise, it can have a great impact on the application\u2019s performance.<\/p>\n<p>If we use the Node server to send static content, like HTML, CSS files, and images, they can hog the single thread. This unfairly affects the dynamic content of the application as they are the ones that actually make use of the application\u2019s inner components and logic. As a result of the unbalanced distribution of execution time between dynamic and static content, our app then considerably slows down.<\/p>\n<p>To avoid this imbalance, you can move the static content from the Node server and use a dedicated middleware, like Nginx, S3, or CDN, to serve them to the users. Then Node gets the opportunity to serve dynamic content that actually makes use of its internal logic and prevents the performance dip static content causes.<\/p>\n<h2 id=\"keep-your-application-stateless\"><span class=\"ez-toc-section\" id=\"Keep_your_application_stateless\"><\/span>Keep your application stateless<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Store any type of data you need to store, like user sessions, user data, and the cache, through the application in external data stores. Don\u2019t store any information in the application itself.<\/p>\n<p>In short, keep your application stateless. You should be able to kill your server every day and restart it without affecting its services to the users. Alternatively to this practice, you can use a serverless platform like AWS Lambda that imposes stateless behavior by default.<\/p>\n<p>Keeping the server stateless prepares the application to survive any eventual system failure without damaging its services and performance.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this post, we discussed 10\u00a0best coding practices\u00a0you should follow as a Node developer. These practices help you to write better code to ensure the best performance of your application.<\/p>\n<p>It\u2019s important to note these are the practices you should follow along with other best coding practices that apply to any programming language. You may not be able to use all these practices the first time you try. Pick the one or two you think are the most important and start from there. Eventually, you\u2019ll be able to follow all of them when writing Node.js code.<\/p>\n<hr\/>\n<p><i><span style=\"font-weight: 400;\">This <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/post\/2020-10-12-best-practices-every-node-developer-should-follow\/\"><i><span style=\"font-weight: 400;\">article<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> was originally published on <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/\"><i><span style=\"font-weight: 400;\">Live Code Stream<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> by <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\"><i><span style=\"font-weight: 400;\">Juan Cruz Martinez<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> (<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">twitter<\/a>: <\/span><\/i><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/twitter.com\/bajcmartinez\"><i><span style=\"font-weight: 400;\">@bajcmartinez<\/span><\/i><\/a><i><span style=\"font-weight: 400;\">), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/livecodestream.dev\/subscribe\"><i><span style=\"font-weight: 400;\">Live Code Stream<\/span><\/i><\/a><i><span style=\"font-weight: 400;\"> is also available as a free weekly <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/news\/\" data-internallinksmanager029f6b8e52c=\"2\" title=\"News\" target=\"_blank\" rel=\"noopener\">news<\/a>letter. Sign up for updates on everything related to programming, AI, and computer <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/sciencee\/\" data-internallinksmanager029f6b8e52c=\"5\" title=\"Science\" target=\"_blank\" rel=\"noopener\">science<\/a> in general.<\/span><\/i><\/p>\n<p class=\"post-article-read-next\">\n    <b>Read next:<\/b><\/p>\n<p>        Watch a VW ID.3 get smashed up in the name of safety    <\/p><\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/thenextweb.com\/syndication\/2020\/10\/22\/here-are-10-best-practices-for-node-developers\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Here are 10 best practices for Node developers&#8221; Node.js is popular among developers as an asynchronous and event-driven language used for backend development. It has a syntax easy enough to understand even for beginners. Whether you\u2019re a beginner or a seasoned developer using Node, following the best practices of Node.js programming is a must when&#8230;<\/p>\n","protected":false},"author":1,"featured_media":95256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/img-cdn.tnwcdn.com\/image\/tnw?filter_last=1&fit=1280,640&url=https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2020\/10\/image-2-11.png&signature=75c662fc096d5bae8f3a8bf1c3d11466","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[73825,75083,76769,76768,76770,76477,73308,70759],"class_list":["post-95255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-application-software","tag-developer","tag-html","tag-javascript","tag-node","tag-programmer","tag-programming-tool","tag-tech"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/95255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=95255"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/95255\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/95256"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=95255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=95255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=95255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}