Node.js — free and open source cross-platform JavaScript framework for server side. Today, thousands of developers use it to develop various applications.
In this article, we will gain a basic understanding of nodejs. We will also learn how to develop small applications in Node.js. But before that, let’s first look into some of its aspects.
It uses event-driven and non-blocking model
Node.js uses the event-driven, non-blocking I/O model that makes it very lightweight and efficient.
Now let’s understand the above statement in more detail.
Event Driven Programming controls flow of any program by the occurrence of the events. By the help of event-listener, function will wait for particular event to occur. In Javascript, onclick, onload, onhover are common event-listener.
Blocking I/O takes time and hence blocks other functions. Javascript being a single threaded and every time a new thread will be kicked to fetch data. This is where Non-Blocking I/O part comes in.
In Non-blocking I/O operations, you can get the output of 2nd request without waiting for response of 1st request. Also, both requests can be initiated at same time. By doing this, there is no need for multiple threads.
Some important terminology in Node.js
NPM — Node Package Manager
Official package manager for node. It is used to install new packages and manage them in useful ways.To install packages via npm, there are 2 modes — local and global.
Let’s say you’re hard at work one day, developing the Next Great Application. You come across a problem, and you decide that it’s time to use that cool library you keep hearing about — let’s use Caolan McMahon’s async as an example. Thankfully,
npm
is very simple to use: you only have to runnpm install async
, and the specified module will be installed in the current directory under./node_modules/
. Once installed to yournode_modules
folder, you’ll be able to userequire()
on them just like they were built-ins. More you can read here.
package.json
This JSON holds various metadata essential to the project. Every Node.js applications should have this file at the root directory to describe the application metadata. A simple package.json file looks like below
{
"name" : "barebones",
"version" : "0.0.0",
}
More for package.json read it here.
Node.js installation
- In Windows/Mac, one can install the node.js via official installer from Node.js download page.
- In Linux OS, one can install node.js via package manager like
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_12.x | bash - apt-get install -y nodejs
Once installed, run below command in terminal/cmd and check the output
node -v
If node.js was installed successfully then you will see the installed version of the node in the terminal.
Sample Example
Once you have installed Node, let’s try building our first web server. Create a file named “app.js”, and paste the following code:
After that, run your web server using node app.js
, visit http://localhost:3000, and you will see a message ‘Hello World’.
This goes on….