10 NodeJs Things You Should Know & Master to be a Pro

NodeJs is a beast on its own, not considering the Javascript side of things. From patterns, best practices, and strategies to work on the Backend, I consider that there are 10 things you should really focus on before you jump into any programming and server-side concepts.

Javascript & NodeJs !== Javascript

Yes, NodeJs is not Javascript and Javascript is the number 1 thing to learn before you get into NodeJs and I shared a list of 10 things you need to focus on when learning Javascript in a different article.

NodeJs is a runtime environment for Javascript, an environment to run Javascript and not a programming language. NodeJs is to Javascript on the Server as Browser is to Javascript on the Client.

It is also important to know when something is part of the environment or the language. For example, it is common to find devs that think that “setTimeout” is part of Javascript, but it is just part of the environment (Window or NodeJs Global).

Streams & Buffer

ArrayBuffer is part of Javascript and in NodeJs, streams are everywhere. Pretty much NodeJs is “built on streams”. Funny enough, streams are one of the most misunderstood things and often regarded as hard to grasp by people coming into NodeJs.

As something so important in Node, it should be a high priority thing to learn about and you cannot talk about streams without mentioning buffer, ArrayBuffer, pipe, back pressure, etc. It is also important to master all the stream types, writable, readable, transform, and duplex.

Streams allow for time and memory-efficient data transferring which is definitely something you want to take advantage of taking into consideration the single-threaded server-side nature of NodeJS.

Events & Event Emitters

When you start working with streams, you can’t help to notice that you listen to stream events and this is because it uses event emitters(event module) to send the various types of events like “error”, “end” and “data”.

Pretty much everything in Node emits Events and you should learn to incorporate events in your application by creating your own events and utilize the event emitter. NodeJs is built around an idiomatic asynchronous event-driven architecture where emitters run the show.

File System

On the server-side, you will have to deal with files by reading them, writing to them, or collecting information about them. That makes the fs (File System) module an important module to master and know your way around it.

You will need to know how to read files and directories, perform CRUD operations, and collect details about them like permission, name, and size. When interacting with the File System you will automatically interact with streams and events as well, which further highlights the need to learn those concepts in depth.

HTTP(S|2)

You are on the Server! That is enough reason to try to learn the HTTP, HTTP2, and HTTPS modules. Many modules have been built on this module like ExpressJs but learning your way around the Node HTTP modules will make you appreciate these modules much more.

HTTP is not just about learning the module itself, it is also about learning how the server-side of things work. How things get to the server (requests), connection (agent), in what format (header, body), what information they contain, how to respond (response) to them, what details you can include are all important concepts to take a deep dive into.

Module & Architecture

You cant talk about Node without talking about the Modular architecture and Modules. The idea that every file is a module (implements the module pattern) with its own scope and you can choose what to expose or not pretty much defines NodeJs applications.

NodeJs Modules work by wrapping the code in your file in a function that injects global things like require, exports, __dirname, and more into your file. Understanding that, will explain a lot about how communication is done, what are the restrictions, emphasizes the need for Events, and facilitates the building of workers and threads.

Global

The global object is essential, and you can’t avoid it. It contains things available on every module like require, exports, and __dirname. It also contains timer functions like setTimeout and setInterval as well as your favorite, the console.

On the browser, you create a global variable(with var) which will be available everywhere (global scope). With NodeJs things are different, the top level is the module itself so, any “global” variable you create is local to the module and you choose what the module exports to the rest of the application which by importing you know exactly what each module needs and where they all come from.

Cluster & Process

The process object is available globally(through the global object) and it provides details on the current running process by NodeJs. It is commonly used to get arguments the NodeJs was executed with, read, and write to the console, etc. For example, the console object wraps around the process for those abilities but you will need to take a deeper dive into process when you work with Cluster.

Cluster allows you to create a cluster of child processes which you can monitor and control. Child processes allow you to take advantage of multi-core systems since NodeJs is single-threaded (Javascript) which means that in a multi-core system, NodeJs will limit itself which may not be enough.

Clusters improve your application by giving it the ability to handle a bigger load and understanding your way around processes allows you to create more powerful applications Cluster processes.

Child Process & Worker Threads

Child process is a different module that allows you to run subprocess. Cluster uses the child process fork method to spawn different processes that allow back and forward communication. Child process is often used to interact with the shell and to run code from a different language like python. It is my favorite way to run shell scripts and commands.

Worker threads are not processes, meaning, it is different from Cluster and Child Process. Worker thread module allows you to execute a thread of javascript code in parallel and it is perfect for CPU intensive tasks that you definitely don't want to run in your main thread.

Worker threads share memory and are perfect for complex data calculation, monitoring and process data for your main application, perform intensive and long database operations, etc. The equivalent on the browser would be the Web Worker.

Path, URL, Util & More

You can’t forget the built-in modules meant to aid you with everything else. The path lets you handle relative paths in your file system. URL helps with anything URL related and the utility module is just a box of magic.

The utility module comes with type checks functions, text encoder, and decoder, util to check deep equality, promisify callbacks, inspect and format various things, etc. I rarely feel the need to install something with these awesome modules and you need to be familiar with them.

Conclusion

Becoming a Pro NodeJs goes beyond these 10 things as NodeJs is so much more. I do believe these are the 10 things to start with if you are heading in that direction though. They allow you to start creating useful things enough to fall in love with NodeJs and further seek more about it.

It goes without saying that practice and experience play a big part and you should try to expose yourself to as many things as possible. Becoming a Pro is a journey and not a destination so take your time to digest things.

YouTube Channel: Before Semicolon

Website: beforesemicolon.com

Blog & YouTube Channel for Web, UI & Software Development - beforesemicolon.comyoutube.com/c/BeforeSemicolon

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store