A brief introduction to NodeJS, its history, usage, and popularity in the current web world.
A brief introduction to NodeJS, its history, usage, and popularity in the current web world.
In the recent few years, NodeJS has become tremendously popular and is one of the most popular runtime environments for web servers. It is an open source, a cross-platform runtime environment for developing server-side applications, or more precisely, code that run on a server.
In this article, we are going to have a look at some of the best practices that developers should follow to keep their huge NodeJS projects maintainable.BEST PRACTICES
Structuring the code base is very important to projects that scale and involve large teams. This ensures that the code is manageable and developers across teams can understand the code. For this, there is a need to establish some ground rules that will be followed by all developers and even testers. As the project grows, the code needs to be broken down, both logically and physically. This ensures that as the project scales up, it stays manageable. A few guidelines as to how to break down a huge code base into smaller and manageable chunks are as following.
The biggest hurdle of large projects is that they tend to grow with each new feature or bug fix. This requires that the project be broken down into different files and then structure those files into directories. Now, this can vary from project to project but the concept is that the project needs to organized such that to make a small change or to add a small feature to one part of the codebase, the developer only needs to navigate to one branch of the directory tree.
With large codebases come a huge set of dependencies. Having one big software with many dependencies is just hard to reason about and this often leads to spaghetti code which is touch or almost impossible to manage. The ultimate solution is to break down the beast into smaller and tamable beasts. This calls to divide the whole stack into self-contained components that don't share files with others, each constitutes very few files and therefore becomes much more manageable. Also, changes to one are completely opaque to another.
This opacity ensures that changes to one module or component in the project do not break the other ones that in some way are using that module or component.
Most of the popular code formatters support popular Code Editors as well. For example, both Prettier and Beautify support Visual Studio Code which is my personal favorite when NodeJS is concerned. Prettier has a VSCode extension called prettier-vscode and unibeautify-vscode.
The official extensions are totally configurable and auto-format the code when the file is saved thereby saving a lot of time and ensure that developers do not lose focus.
Not using any of these code formatters will not affect the coding efficiency in any way but it definitely affects the developers’ efficiency. Developers will focus on tedious spacing and line-width concerns and time might be wasted overthinking the project's code style.
According to this principle, it is recommended to have named functions over anonymous inline functions. It suggests you to always create named functions, including closures and callbacks and name them logically. An example would be more appropriate for explaining this. Let’s have a look at an example.
In the code snippet on the left in the above set, you can see that once the createOrder method is initiated, it executes certain operations in a certain order but it all happens inside an anonymous function. In the code on the right, instead of creating an anonymous function, we create a named function called postOrderTasks() that only does those tasks in the same order. The primary advantage here is that this function can be re-used over an order again in other parts of the project as well and this reduces code redundancy.
Moreover, if you find a bug in the code that creates an order, you fix that bug only in once place i.e. postOrderTasks() method and it will reflect in all places - saves time.
Another advantage of using named functions is that when you are profiling a NodeJS app, it allows you to easily understand what you're looking at when checking a memory snapshot for memory leaks or inefficient code. Debugging production issues using a core dump (memory snapshot) might become challenging if you do use too many anonymous functions.
It is generally considered a good practice to follow a naming convention while naming all variables, objects, and classes but the case of the names and the characters in the names is also very important as lower and upper cases represent different meanings.
It is recommended to use lowerCamelCase when naming constants, variables, and functions and UpperCamelCase (capital first letter as well) when naming classes.
This is universally accepted conventions and developers across the globe can easily identify and differentiate objects and classes when using this naming convention.
Article article = new Article();
Article firstArticle = new Article();
NodeJS 8 and higher have to fill Async-await support in their LTS releases. It is often a good practice to have async-await operators instead of using multiple callbacks. Inspired by the C#’s async-await operators, NodeJS offers Async-await operators that appear to block asynchronous operations waiting for the results before continuing with the following statement where synchronous execution is a requirement.
Async-await in NodeJS is completely non-blocking although it may appear at blocking. It is fast and efficient and works very similar to C#’s async-await operators.
Let’s have a look at an example. Following the code that uses traditional callback pattern to execute asynchronous synchronously.
In the above code block, there are 3 asynchronous functions executing in order and therefore the code looks like a staircase. With more functions, this looks terrible and code readability becomes very poor. Async-await comes to the rescue from callback hell. Now, let’s rewrite the code using the async-await operators.
As you can see, the code is so much more readable. The function foo() does not block NodeJS’s event loop, in spite of its synchronous appearance. Execution within the function is suspended during each of its three asynchronous operations, but NodeJS's event loop can execute other code whilst those operations are being executed.
Using the await operator next to an asynchronous function call ensures that the next line of code will only be executed when the awaited function is executed successfully.
It is recommended to use try-catch blocks when using async-await operators which we will discuss next.
This is more of a requirement than a recommendation. When using async-await operators, we no longer have the access to success and failure callbacks in the promises - then() and catch(). How do we handle errors then? Well, we fall back to the old try-catch method of handling errors in the code.
Wrap the code with the await keywords in a try block and correspondingly write the error handling code in the catch block. Simple right? Let’s rewrite the above code block that uses async-await using the try-catch.
Simple, elegant and the code stays readable. Keep in mind that if you do not recommend you awaited calls in try blocks, you may not be able to find if you function executed successfully or failed. In such a situation, you will not be able to handle errors.
Whether the software is working perfectly or not, logs are an avoidable piece of information. Always use a logger tool that logs information about databases accesses, crashes, user access patterns and other information that might be useful for the teams. There are many sophisticated tools available out there for NodeJS. Some of the most popular ones are -
I have used Winston in numerous projects and it is one of the best but it varies from project to project and from developer to developer as well.
With Winston, you can:
Multiple modes of transport allow you to log to files, network, or pretty much anything. There are several core modes of transport included in Winston, which leverage the built-in networking and file I/O offered by Node.js core. In addition, there are additional transports written by members of the community.
Without a logger, skimming through console logs or manually through messy text file without querying tools or a decent log viewer can be a pain when your software is down.
There may be cases when the server crashes or run into errors that it cannot handle since we, as developers, have not considered certain edge cases. In these situations, we need to ensure that the process is terminated gracefully, and restarted immediately without no or minimal downtime. Information about the crash should also be written to logs so that we can handle the crash whenever it occurs in the future or eliminate it altogether.
Process monitors or process management tools like PM2 are available for NodeJS that handle all of these things for us. PM2 utility also integrates a load balancer. You can keep application server processes alive and reload/restart them with zero downtime.
PM2 allows you to easily manage your application’s logs as well. You can display the logs coming from all your applications in real-time, flush them, and reload them. There are also different ways to configure how PM2 will handle your logs (separated in different files, merged, with timestamp…) without modifying anything in your code.
Primary features of PM2 are -
You can also check out forever or supervisor which offer similar features for process monitoring.
Environment variables are a fundamental part of developing with NodeJS, allowing your app to behave differently based on the environment you want them to run in. These are what the name implies, variables stored in the environment in which the code is being executed. These variables can be used to store sensitive information like API keys or secret strings and they can also be used to store other information to indicate if the app is in test, dev or production mode. Based on the values of these variables, different code blocks can be executed to perform different operations or perform operations differently.
Environment variables can be used to store app-specific keys, software version and build information, files paths and folder paths, ports and host information etc. When the app is deployed on another server or on the cloud, the environment variables ensure that the app still works but now, since the environment is different, it will work exactly as it is supposed to work. You are saved from the trouble of switching variables before deploying the app.
There is a specific package called dotenv that is very popular and can be used to read .env files that you can use to use environment variables. Environment variables ensure that your code is always aware of the environment it is running in and can work in ways as intended.
Keep in mind that these files should be ignored when you push the code to version control like Github.
It is always recommended to use the LTS version of the latest available NodeJS version. LTS stands for Long term support which means that no matter what happens, you will get support from the NodeJS team for this version in the future. It is future-proof and therefore is suitable to be used in production.
The other variant, which is called Stable version, receive frequent updates, bug fixes and performance improvements which can be breaking at times. If you want the most stable version, use the latest available LTS.
According to Rod Vagg from NodeJS LTS team, the point of establishing an LTS plan for Node is to build on top of an existing stable release cycle by delivering new versions on a predictable schedule that have a clearly defined extended support lifecycle. While this may seem at odds with the open source tradition of “release early, release often”, it is an essential requirement for enterprise application development and operations teams.
Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user.
In NodeJS, XSS attacks can be prevented by using dedicated modules. There are a lot of modules available for this very purpose. The application should be using secure headers to prevent attackers. These can be configured easily using modules like a helmet. There are other packages like sanitizer and dompurify that ensure that the content is sent down to the client as pure content, and it cannot be evaluated. Basically, this is mitigated by using dedicated libraries that explicitly mark the data as pure content that should never get executed.
The Node process will crash when errors are not handled. Many best practices even recommend to exit even though an error was caught and got handled. This is because if not crashes, this opens a very sweet attack spot for attackers who recognize what input makes the process crash and repeatedly send the same request.
A denial-of-service attack (DoS attack) is a cyber-attack in which the perpetrator seeks to make a machine or network resource unavailable to its intended users by temporarily or indefinitely disrupting services of a host connected to the Internet.
There is no one solution to this because there is a human sitting at the attacking end but there are a few things that can help.
Crashing does not look all bad now, does it?
ESLint is a standard tool for checking possible code errors and fixing code style issues. It is not only used to identify minor spacing issues but also to detect serious code antipatterns like developers throwing errors without classification or not writing a return statement in the method that is supposed to return something. ESLint can automatically fix code styles, but it is often used with other tools like prettier and beautify.
Linting forces developers to follow standard practices and therefore makes development easier and coherent for everyone working on the project. In my personal experience, linting has made me a better developer overall.
We, at HireFullStackDeveloperIndia, implement the right strategic approach to offer a wide variety through customized Vue.js development services to suit your requirements at most competitive prices.
Vue.js is a scalable framework with a robust in-build stack that can extend itself to operate apps of any proportion. Moreover, vue.js is the best framework to seamlessly create astonishing single-page applications.
Our Vue.js developers have gained tremendous expertise by delivering services to clients worldwide over multiple industries in the area of front-end development. Our adept developers are experts in Vue development and can provide the best value-added user interfaces and web apps.
We assure our clients to have a prime user interface that reaches end-users and target the audience with the exceptional user experience across a variety of devices and platforms. Our expert team of developers serves your business to move ahead on the path of success, where your enterprise can have an advantage over others.
Here are some key benefits that you can avail when you decide to hire vue.js developers in USA from HireFullStackDeveloperIndia:
If you are looking to hire React Native developers in USA, then choosing HireFullStackDeveloperIndia would be the best as we offer some of the best talents when it comes to Vue.js.
Learn Node.js API Development from absolute scratch. This video is for complete beginners getting started guide!