Akash Rajput Technology over a cup of coffee

Zero to Hero — Node.js (Part — 1)

2 min read

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.

const _fs= require('fs');
let _contents = _fs.readFileSync('package.json').toString();
console.log(_contents);

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.

const _fs = require('fs');
_fs.readFile('package.json', function (_err, _buf){
console.log(_buf.toString());
});

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 run npm install async, and the specified module will be installed in the current directory under ./node_modules/. Once installed to your node_modules folder, you’ll be able to use require() 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:

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

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….

  1. Zero to Hero — Node.js (part-2)
  2. Zero to Hero — Node.js (part-3) – Frameworks
  3. Zero to Hero – Node.js (part -4) – DB Connection
  4. Zero to Hero – Node.js (part -5) – REST API
  5. Zero to Hero – Node.js (part -6) – Secure Rest API
Akash Rajput Technology over a cup of coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss a story from me, get updates directly in your inbox.
Loading