Introduction to Node.js
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
Reading time: 20 minutes
In this article, we will take you through the basic idea of NodeJS and various ideas such as NodeJS's package manager NPM, Installation guide and a basic example along with When to use NodeJS? and when not to use?.
⭐ Introduction to Node.js
Going with the official definition - NodeJS is a JavaScript Runtime built on Google's open-source V8 JavaScript engine.
Now, what does that actually mean? 😕
Well, let's start by trying to understand what the JavaScript Runtime and the V8 engine actually are.
So, you have probably already used JavaScript before and it was probably always just inside a browser, right? Because any browser natively understands HTML, CSS, and JavaScript and no matter if you write vanilla JavaScript or some JavaScript framework like React or Angular code, that's all just JavaScript that gets executed right in the browser.
So, in this case, the browser is then the JavaScript Runtime.
But, what if we could take JavaScript out of the browser and simply execute our JavaScript code somewhere else without all the restrictions that we have in the browser? 🤔
Well, it turns out that we actually can.🎉
And the solution for this, as you can guess, is called NodeJS and so Node JS is just another JavaScript Runtime.
It's just like a container, like an environment, in which a program written in JavaScript can be executed, but outside of any browser whatsoever.
Now, who actually does execute the code if not the browser? And that's where the V8 engine developed by Google comes into play.Because that is exactly where JavaScript code will be parsed and run in NodeJS.
So, I hope that now the definition of NodeJS being a JavaScript Runtime on the V8 JavaScript engine makes a lot more sense and again, this is just a very high level overview of what Node is and how it works.
⭐ JavaScript on the Server :
Okay, So now that we have JavaScript outside of the browser in a kind of stand alone environment which is NodeJS, we can do so many more things with JavaScript that were completely impossible before, like accessing the file system,or better networking capabilities, and all these factors together give us,the perfect conditions for using NodeJS as a web server
This means that now we can finally use JavaScript on the server side of web development in order to build fast, highly scalable network applications for powering the back-end all for websites or web applications.
💫 And this is absolutely fantastic and game-changing for web development.
⭐ Pros of using Node.js :
Now let’s see some Pros of using Node.js
- Single threaded based on event driven, non-blocking I/O model.
- Perfect for building fast and scalable data-intensive applications.
- Companies like Netflix, Uber, Ebay and Paypal have started using it in production.
- JavaScript is used across the entire stack- hence, much faster and efficient development.
- Another great reason for using Node.js is the presence of huge library of open-source packages, or modules, available for everyone for free.It is called npm and you’ve probably used it.
- Very active developer community that are constantly improving Node.js and helping beginners like us.
⭐ Where to use Node.js :
We know that Node.js is perfect for building fast and scalable data-intensive applications, which makes it a perfect fir for building applications like
- Building an API with database behind it, and preferably a NoSql database like MongoDb.
- Data-streaming applications (like Youtube or Netflix).
- Real-time chat applications.
- Server-side web applications.
⭐ Where not to use Node.js :
Great, we are going well, but we should also be aware of where to not use Node.js.
There are some apps that we should not build with Node.js. And this is when our app needs some super heavy server-side processing like having:
- image manipulations
- video conversion
- file compression or anything like that.
So, in this case, we're better off using something like Ruby on Rails, PHP, or Python. Because NodeJS just wasn't developed for these use cases and although there are solutions for dealing with them, we should always use the right tool for the task at hand.
⭐ Node Package Manager (npm) -
The way we write JavaScript has changed tremendously during the last couple of years. Today we compile ES6 and even newer JavaScript versions down to ES5. We use module bundlers, task runners, external packages and other dev tools.
Modern JavaScript is not about the language itself, but it is more about the JavaScript ecosystem and the environment that we used to write in. So, we still write the exact same JavaScript, but we use it together with a set of tools that make it easier and better to work with.
And the foundation of all these tools is the Node Js and NPM ecosystem.
In this ecosystem, we can find all kinds of third party open source tools and libraries and frameworks needed for modern web development.
Now, in order to use and share these packages, we need some kind of tool to install and manage them. And that's where the node package manager, or NPM, comes in.
So, npm is just a simple command line interface that allows us to install and manage third party open source tools, libraries and frameworks needed for modern and real life web development.
In addition, NPM also allows us to write scripts to use our development tools.
⭐ Installation of Node.js and npm -
For Ubuntu, you can install Node.js using the following steps:
sudo apt-get install curl python-software-properties
curl -sL https://deb.nodesource.com/setup_12.0 | sudo -E bash -
sudo apt-get install nodejs
- For Windows, Download and install the most recommended version of node.js from here..
For other system configurations, download and install the binary files from here.
- After the installation is completed, open the terminal to confirm if node.js and npm have been installed correctly.Node package manager comes with node itself.
node -v
npm -v
If there’s an error, this means there’s some error in the installation process.
⭐ Modules in Node.js
Modules in Node.js are similar to javaScript libraries. They are some set of functions that we wish to include in our application.
No further installation is necessary for using these modules. Some of the modules are -
-
http- http is a built-in module in Node.js and is used to transfer data over the Hypertext Transfer Protocol (HTTP).It is also used to create web servers that listens to requests and send back responses.
-
fs - fs stands for File System. This modules enables us to work with file system on our computer. It lets us create,update,delete and update files of our system.
Including modules -
To include any of the module, we can use the require() method:
For eg.
const http = require('http');
You can read more about Node.js modules and their usage here.
⭐ Hello World in Node.js
Ofcourse, we had to this, don't we ?
So, Create file index.js and add the following code to it.
console.log("Hello World!");
Open your node terminal/command line interface, change the directory to the folder where the file is saved and run
node index.js
You will see "Hello World" printed on your screen.
Hurray ! 🎉
You just wrote Hello World in Node.js.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.