Intro to Node.js

Intro to Node.js

Β·

4 min read

Featured on daily.dev

Hey everyone! πŸ‘‹πŸΌ

A week ago, I started learning Node.js by myself as a part of my new year's resolution 😬. To actually make it work, I decided to document my learning in several blog posts. This would be my first ever technical article, and what better topic than back-end development technology could I, as an aspiring web developer begin with? πŸ€ͺ

bucket.jpg

Contents πŸ“œ

Getting Started

Installing Node.js is very easy and independent of what OS your PC has. Head over to https://nodejs.org/en/ where you should see two green buttons for downloading Node.js. The OS in your PC is automatically detected and a suitable version is displayed for you.

To make sure you have installed Node.js, open your terminal and type node -v which should display the version of your Node.js, if it's installed successfully.


What is Node.js?

This is what you'd find on Google:

Node.js is a JavaScript runtime built on Google's open-source V8 JavaScript engine.

Sounds really complex right? 😨 Let's break that down.

What is a JavaScript runtime?

A JavaScript runtime is where you execute your JavaScript code. Normally, your HTML, CSS and JavaScript in frameworks like Angular, React etc., are converted to plain JavaScript and executed in the browser; making the browser as our runtime.

Now, if we take out JavaScript from the browser and run it somewhere else devoid of the restrictions browsers pose to us, it widens our horizons. We can do many things that are restricted by browsers like accessing file systems, better network capability etc., which is exactly what Node.js does.

So...

Node.js is a standalone environment (think of it as a container) where we isolate our JavaScript. Google's open-source V8 engine parses the JavaScript and executes it. This is a very surface-level understanding of the concept. I will be explaining it in more depth in the coming parts of the series.

πŸ’‘ Remember: Node.js is not a framework and it’s not a programming language. It is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser.


Why and when to use it? 🧐

Here's why:

Don't worry if you don't understand the jargon, it will be explained in coming posts. Stay tuned!

  • Single-threaded, based on event-driven, non-blocking I/O model 🀯

  • Perfect for building fast and scalable data-intensive apps ⚑️

  • Companies like Netflix, Uber, PayPal, eBay, Walmart, LinkedIn have started using node in production πŸ’―

  • JavaScript across the entire stack: faster and more efficient development πŸ‘©πŸ»β€πŸ’»

0_MfxqrK2yGag7up0X.jpg

  • NPM (Node Package Manager) - huge library of packages available for everyone for free πŸ€‘

node-modules-meme (1).jpg

  • Very active developer community πŸ‘¨πŸ»β€πŸ«πŸ‘©πŸ»β€πŸ«

βœ… Use Node.js for:

  • API with database behind it (preferably NoSQL)

  • Data Streaming (think YouTube)

  • Real-time chat application

  • Server-side web application

❌ Don't use Node.js for:

  • Applications with heavy server-side processing (CPU-intensive). Instead opt for: Ruby on Rails, PHP, Python.

0_0wN-HNbQpH578V4r.jpg


Some basic commands in REPL

Now that we know what Node.js is and why we need it, let's start using it!

Open your terminal and cd into your working directory.

Type node and hit Enter. This will open up Node REPL (Read-Eval-Print Loop). In here, you can write JavaScript code just like in a normal terminal. As we know that Node.js is a JavaScript runtime, we are going to run it out of the browser. It also supports JavaScript ES6.

Use .exit or Ctrl+D (if you're on Windows)/Cmd+D (if you're on iOS) to exit the REPL.

For example,

node1.PNG

Hit Tab twice to view all the global variables, modules etc., in Node.

node1.PNG

You can also view methods relating to each variable. For eg., use String. and hit Tab twice to see all methods related to String constructor.

node1.PNG

So, this is how Node.js REPL is a command-line tool that can be run as a regular web server and lets one run JavaScript programs.


You've reached the end of the article 😊 Thanks for sticking around. Stay tuned for the next part! πŸš€