Learning Node.js š¤© š¤© š¤©
Introduction
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.
Ryan Dahl initially wrote Node.js in 2009. The idea being, you could use Javascript to write scalable network applications. He also wanted to create real-time websites(super responsive and reactive websites) with push capabilities(a form of communication). We use these technologies today, which make for a much better user experience. Node.js also has a great advantage for developers as they would only need to learn one language for full-stack development.
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.
Yet Node.jsās success is also credited to the impressive implementation of Googleās V8 engine. This Javascript & Web Assembly Engine in C++ was a much-needed improvement. Basically, Javascript got a huge upgrade in back-end development. Allowing it to tap into machine-level code and optimize tasks.
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?
Everyone learns Javascript at some point in their developer career path. Chances are, youāll make a few web-based projects or even smartphone applications. Youāll want to impress people with projects like SPAs(Single Page Application) or Chat Apps which are great to build with 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
Setup
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)
https://nodejs.org/en/
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).
In terminal:
node -vREPL:
node
Congrats, you got a Node.js setup. Pretty easy, right?
Conclusion
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
Node.js here:https://nodejs.org/en/
Grab Vscode here: https://code.visualstudio.com/