Hello Fellow readers. Recently I’ve taken a break from LeetCode explanations and wanted to share with you a new technology I’ve been learning in my spare time. In this blog, I will give you a general understanding of Node.js’ Runtime environment, its benefits, and how to set Node.js on VScode.
I think the best way to understand something is to ask informal questions. It gives us a really personal way to grasp the material. Questions will help me flesh out the topic. We want to understand the general idea here, so we can explain(most importantly in your own words) and justify why it’s worth your time to learn it! So grab a coffee or tea, put on some chill ambient music, and let’s dive into a causal read about Node.js.
What is Node.js?
Note on runtime: a runtime environment is the environment in which a program or application is executed. It's the hardware and software infrastructure that supports the running of a particular codebase in real time.
It’s important to note that it wasn’t the first runtime environment(sandbox where code is executed) to attempt this. That credit goes to Netscape and its LiveWire Pro Web about 13 years ago. Like most things in science, people build off each other's successes and failures, advancing the concept in an objective fashion. We see this with Dahl’s Node.js.
The big takeaway here is being you have a Non-blocking(it’s a multi-tasker, little or no waiting) and Event-driven (always listening to the server looking and reacting) “philosophy” when dealing with back-end problems. This creates an incredible user experience, so dang good you don’t even notice it unless something breaks!
How it works
Oh boy, let’s try to boil this down(Not an expert, just a huge dorky fanboy here🤓 ).
Node.js is an excellent listener. It does this by using an Event Loop. This loop runs infinitely, constantly listening to your created server. If it breaks, Node.js stops working.
Node.js is also really good at dividing tasks that take a lot of time or are very expensive in computation(code that slows it down). It does this by listening to callback functions or functions that fire off based on certain circumstances(common in computer code, think like a specialized lego piece).
The Event loop runs in phases. Yet can jump backward if it needs to resolve any outstanding tasks that need to be done. Each phase is meant to divide its given tasks efficiently for better performance and user experience. Timers, Pending Callbacks, Poll, Check, and Close Callbacks are all phases that it works through. Without getting too much into detail, I will say that it’s a very meticulous process, which accounts for alot of complicated factors in your backend. Don’t think of it as Node.js magic but rather a complicated dance move that dots all the “i”s and crosses all the “t”s.
There is also a component known as the Worker Pool. This component does most of the heavy lifting, such as traversing through your file system or resolving complex and time-consuming code. The worker pool can also tap into additional threads from your CPU, thanks to the C++ magic from the V8 engine. This can give it the additional strength it needs to tear through all the work that ends to get done. When it finishes this code, it talks to the Event Loop with a callback function. Making work delegation and execution a team effort!
I like to think of lazy(Event Loop) vs. hardworking mindset(Worker Pool) workers. Lazy folks like to do many easy tasks. Completing multiple tasks feels satisfying, while hard-working folks focus all efforts on what’s in front of them and seek the challenge. They both do work, but how we manage them creates a level of productivity that’s impressive. Node.js is that manager.
The Event Loop and the Worker Pool both fire up automatically in your Node.js code. This isn’t something you deal with when you work with Node.js. It’s a broad-strokes 🎨 🖌 explanation of what’s going on under the hood.
Why use Node.js?
Apps tend to be front-end heavy, and you’ll do alot of the work in the front-end as you most likely building for user experience. The more features you add, the more front-end work you’ll need to do. Node.js is a good choice in this environment, letting you focus on those important features.
Node.js is also pretty good at scalability. It’s baked into the design, thanks to its philosophy or paradigm. That’s important in tech as you build your user base. You never know if your next project becomes the new Facebook; having this already setup makes your life much easier!
Node.js offers an easier learning path as you won’t need to take time learning a new language to get your project off the ground. The Combination of Node.js + Express(Web framework for Node.js) is a real great way to understand what’s going on server-side with your application.
Coming from a Ruby on Rails background, alot of the back-end technical stuff gets abstracted out(simplified for user experience), which is nice for the day-to-day coder. Yet you don’t get an appreciation or deeper understanding of back-end setup like you would with Node.js. You’ll build alot more on your own, which makes it a great experience for the more seasoned developer.
Companies building with Node.js- Netflix- PayPal- Trello- Capital One- LinkedIn- Yahoo- Mozilla- Uber
Have I sold you yet on learning Node.js? Awesome, please allow me to walk you through an install. Chances are you may already have Node.js locally, thanks to NPM. Let’s check your IDE and see what the latest version is. I use VScode; it’s a pretty common IDE for developers. If you are interested, click on the link below and install before you go through these instructions.
check in your terminalnode -v
If it isn’t the latest version, let’s download it from the Node.js website. Click on the latest version based on the current platform your running (OS or Windows)
Now follow the installer's guide. After that, let’s recheck our terminal to make sure everything is working as it should. We want to make sure we got the right version. We also want to make sure the Node.js is working by entering its baked-in REPL(testing environment).
Congrats, you got a Node.js setup. Pretty easy, right?
Big disclaimer here, not an expert. Just writing about the broad strokes and greatest hits on Node.js. If you want a comprehensive understanding of this topic, please check the links section below for additional resources. It never hurts to read from multiple resources, right ;).
Node.js is awesome for App development. I encourage you to learn more using online documents or courses. Node.js is a universe in itself and really worth taking the time to build, learn, and grow within it as a professional.
Check out these Hot Links:
More on Node.js: https://en.wikipedia.org/wiki/Node.js
Runtime Enviroment/Runtime System: https://en.wikipedia.org/wiki/Runtime_system
Grab Vscode here: https://code.visualstudio.com/