Node js Best Practices: All of us here will agree that it is easy to build a Node.js application. But the real challenge comes when it is tried to be implemented. This article concentrates on helping developers run the Node.js application once it is created. Most important acts you can consider would be:
Use reverse proxy – Node js Best Practices
A reverse proxy can be used to avoid not being able to run multiple nodejs apps on the same server, exposing your real port on which everything runs, etc. Thus your app will be secure and easy to use for the users. You can easily develop reverse proxy using Node.js
Use Monitoring tools
Yes, we all dream about developing that one application without any kind of errors! But how much ever we try it always is a dream as there will some bug or crashes that occurs. It is OK to have bugs and crashes, but it Isn’t OK to not be notified about it and correct it immediately. Hence, you have to ensure a very powerful monitoring tool is employed. A few such examples would be:
Keymetrics which uses PM2 and Trace by RisingStack
Node js Best Practices – Remove console.log statements
We understand that console.log is a favorite amongst developers as it helps identify bugs and saves out time. But in the real picture, that is during production, it consumes a lot of CPU time and wastes the resources. Hence, we always suggest that you should just consider removing the console.log statements in production.
Use external storage for global data
We restart the server very often during the initial stages. Using temporary storages during such times will mean that data is erased every time we restart and we have consume more time to build everything from scratch each time we restart. Hence, its only wise to use fast external storage such as Redis to store data.
Recheck security measures
Security as we mentioned previously as well plays a crucial role in deciding the efficiency of your app. You have to time and again make sure that your app meets all the security standards.
Security is the main concern while developing an application as that is one of the top functionalities that users look at while accessing your app. You must therefore use SSLs that provide an extra layer of security by encrypting the data that is being transmitted. You should install the SSL on the reverse proxy and let the outside world use your node app via the reverse proxy.
Ensure that the real app is accessible only by private users
To keep your app safer you have to be sure that only the reverse proxy server can communicate with your app and no one else from the real world is able to do the same. This can prevent the attacks and many security loopholes in your app. You can also restrict SSH access to specific network or VPN to prevent outsiders accessing your app.
Run Node.js in cluster mode
It is very important in production that you run your app in the cluster mode.It allows you to fork between multiple smaller processes of the same process. This ensures zero downtime and high availability. That is why you must always run the app in cluster mode.
Perform more I/O and less CPU intensive task
We all know that Node.js runs on single threaded event loop and it is best suited for I/O intensive operation. Thus, while building your app using node.js keep in mind to use more of I/O intensive tasks rather than preferring the CPU intensive ones.
The app once out of the development stage should always use process managers as it is required to be restarted several times during the production in case of exceptions.