Table of Contents
Node .js is one of the most popular server-side frameworks. Many companies are adopting node.js as their server-side language for developing their web applications. Some popular examples are Paypal, YouTube, Amazon, Walmart, Reddit, Netflix, etc. In this tutorial, we will learn the basics of getting started with Node.js. So let us proceed with our tutorial by looking at the benefits of using Node.js.
Why Node.js is so popular
There are many server-side languages like Ruby, Python, PHP, Java, ASP NET, etc so why the need arises for Node.js. Before Node.js javascript is only used inside the browser to provide basic interactivity of the web pages such as dropdown. But in the year 2009, Some brilliant programmers took out the powerful V8 engine of Chrome, a powerful javascript engine, and combine it with C++ to make it run on the server-side of a website. Many developers found this concept interesting and have contributed to the project, this led to Node.Js as how we see it today.
The main benefits of Node.js are:
- It is based on chrome’s popular V8 Engine, which is very fast and can run thousands of instructions per second.
- There are many libraries available in node js that makes our task easier.
- By default, Node Js executes code asynchronously, this means that it does not wait for an instruction to be completed and will move to the next instruction.
- Developers love this because they can use the same language for both server-side and client-side development. So they have to learn only one language for both the backend and frontend.
- Node.js has huge community support and many packages are available that can be installed and used in our projects using npm, a powerful package manager for node js.
Installation
To install node js in your system go to the download page of Node and download the appropriate version of the installer for your system.
After downloading the installer, run it and install the node in your system. You can check your installation by firing up your terminal and running the following command.
If the above command runs successfully and displays the version of the node installed then the installation is successful. The installer also installs the node’s package manager npm. You can also check the version of npm installed by running the following command in the terminal.
The above code will display the version of the npm installed in your system. If both the commands run successfully, then the node has been successfully installed in your system. Now we can use it to run Javascript.
Running Javascript Using Node
Let us see how we can run javascript codes using node. We can run javascript in two ways one is by running single line code directly in the terminal by opening node and the second is by writing the code in a file and then run the file using node.
To run javascript codes directly in the terminal we need to open the terminal and execute the node command in the terminal. This will open the node command shell, as shown in the below image.
After opening the node shell, we can run any javascript command in the shell. For Example, to print “Hello World” in the console we need to run the following code in node.
console.log("Hello World")
On running the above code in the node shell, the string “Hello World” will be displayed in the console. Look at the below image for illustration.
Running code from the shell is good for single line code, but it will be complex to write code in the node shel while writing a multiline code. So we need to write multiline javascript code into a file having the extension js and run it using the node command. So let us see how to do this, create a javascript file with the name index.js and copy the following code into the file.
// creating some variables var a = 10; var b = 20; // performing addition, multiplication and substraction var sum = a + b; var mul = a*b; var diff = a-b; // displaying the result console.log(`The sum is ${sum}`); console.log(`The difference is ${diff}`); console.log(`The product is ${mul}`);
After copying the above code, open the terminal and run the command node following the file name shown in the image below.
In the above image, we can see that the code inside the javascript file has been executed and the output has been displayed in the console.
Using Built-in Modules
In Node, we can also use modules. They are similar to javascript libraries and can be used to add many functionalities to our code. We can use modules in our javascript code by using the require() function of javascript. There are many built-in modules in node js like http, os, path, etc.
Let us see an example of using a module. First, copy the following code into the index.js file.
// importing the required modules const os = require('os') // This will return the path of the home directory const home = os.homedir() // This will return the platform in which the code is run const platform = os.platform() // This will return the Operating System Version const version = os.version() // displaying the informations console.log("The path for home is : " + home) console.log("The platform is : " + platform) console.log("The version of OS is : " + version)
In the first line of the above code, we use the require() function to import the os module in the index.js file. The os module contains many functions such as homedir(), platform(), version(), etc. These functions help us collect operating system info and perform some system tasks using the module. In the above code, we use the homedir() function that’s used to get the path of the home directory. Next, we use the platform() function that is used to get the platform in which the code is running eg:- posix, win32, etc. We also used the version() function this is used to get the OS version. At last, we use the console.log() function to display all this information to the console.
On running the above code, we will get the output as shown in the below image.
There are a huge number of modules available in the node js. You can see a full list of all the available modules and their uses in the official documentation of Node Js.
Conclusion
In this tutorial, we have learned the basics of Node Js. We have seen how to install node js on our system and then how to use it to run javascript code in the terminal. We have also take a look at how to use the built-in modules in Node Js. You may also want to see our guide on how to add a dark mode toggle button to a website.
Source: https://www.codeunderscored.com/getting-started-with-node-js/