Beginners Guide to Node.JS

Written by mahesh-sharma | Published 2020/08/25
Tech Story Tags: nodejs | framework | javascript | filesystem | npm | software-development | backend | nodejs-apps

TLDR NodeJS is an open-source framework used for server-side applications in JavaScript. Node.JS is platform-independent. Node.js can be easily installed and deployed on different platforms, such as Windows, MAC, and Linux. It is based on Google's V8 JavaScript Engine. NodeJS can adopt a single-threaded model with the aid of event looping. NodeJS never buffers data. It can be used to collect form (summary) data and collect data from the server. The NodeJS module is a simple or complex functionality structured in single or complex JavaScript files that can be reused.via the TL;DR App

Introduction to Node.JS
In this article, we are going to give a brief description of Node JS.
Table of Content Node.Js
  • What exactly is Node JS?
  • Use of node js?
  • Node js features
  • Node package manager (NPM)
  • Node js modules
  • Export module in node js
  • Node js express framework
  • Node js and NoSQL database
What exactly is Node JS?
It is an open-source framework used for server-side applications in JavaScript. In this, you can directly run your JavaScript program on the server. Node.JS is platform-independent. Node JS is written in the C++ programming language.
Uses of node.js
  • It is used to generate dynamic pages like the content.
  • It directly manipulates the data on the server through node js.
  • It is used to collect form (summary) data.
Node JS Features
There are certain features of node js and some of them are listed below:
  • It is an open-source MIT framework license that is funded by a wide community. The group has led to the introduction of new technologies to Node.js applications.
  • It is simple and fast since Node.js is based on Google's V8 JavaScript Engine.
  • The next feature is Asynchronous. All Node.js libraries are asynchronous, which means Node.js based servers never wait for the API to send back the answer and switch to the next API.
  • Node.js can adopt a single-threaded model with the aid of event looping.
  • Node.js, the most important feature, is that it never buffers data.
  • Node.js can be easily installed and deployed on different platforms, such as Windows, MAC, and Linux.
Node package manager (NPM)
NPM handles all Node.js packages and modules and consists of the npm client command line. This will be built on the Node.js development system. The appropriate packages and modules for the Node project are built using NPM.
Node.js Modules
Node.js Module is a simple or complex functionality structured in single or multiple JavaScript files that can be reused around the Node.js application.
Node.js contains three types of modules:
  • Core Modules
  • Local Modules
  • Third-Party Modules
Node.js Core Modules
The core modules are compiled into its binary distribution and are loaded automatically when the Node.js process begins. Some of the essential core modules in Node.js are described in the following table:
Module:
1. HTTP (Hypertext Transfer Protocol)
Description: The HTTP module contains classes, methods, and events for building the Node.js HTTP server.
2. URL (Uniform Resource Locator):
Description: The URL module contains URL resolution and parsing methods.
3. query string:
Description: The query string module provides methods for dealing with the query string.
4. path:
Description: The path module provides methods to manage the paths of the script.
5. Fs:
Description: The fs module contains classes, methods, and events for working with I / O files.
Loading Core Modules
Syntax
var module = require('module_name');
Node.js Local Module
Local modules are locally created in your Node.js application. These modules contain the various features of your application in separate files and directories.
Let's take an example to write a simple logging module that logs the information, alert or error to the console.
Example:
var log = {
            info: function (info) { 
                console.log('About_Information: ' + info);
            },
            warning:function (warning) { 
                console.log('About_Warning: ' + warning);
            },
            error:function (error) { 
                console.log('About_Error: ' + error);
            }
    };
module.exports = log
In the above program, we created an object with three methods-info(), warning(), and error(). In the end, we assigned the object to module.exports.
Loading Local Module
You need to load the require() function in the same way as the core module to use local modules in your application.
Example:
var myLogModule = require('./Log.js');
myLogModule.info('Node.js started');  
In the above example, it loads the logging module using require() method and the specified path where the logging module is stored.
External modules (third party module)
You can only use external modules by installing them via NPM. Such modules are usually built free to use for other developers.
Globally Including the Third Party Modules:
npm install --g <module_name>
Include the module file in your main application package:
npm install --save <module_name>
Export module in Node.js
Module.exports or exports is a special object that is used by default in any JS file in the Node.js program.
Let’s take a simple example of reference in node js:
Syntax
var object1= {
  Name: "Mary",
  Course: "BBA"
}
var new_object= object1;
new_object.Name= "james";
console.log(object1.Name);
output
Let’s take an example to describe the use of double equal to and triple equal to in node js:
Double equal is used to check the syntax only, but triple equal is used to check syntax and data type.
Example of double equal to:
Syntax
console.log(10== "10");
Output
Example of triple equal to:
Syntax
console.log(10=== "10");
Output
This function in node js:
var object1= {
  Print_name: function() {
    console.log(this=== object1)
  }
};
object1.Print_name();
output
Node js prototyping
Syntax
function Student(name) {
  this.name= name
}
Student.prototype= {
  geet: function() {
    console.log("hello my name is: " + this.name)
  }
}
var frank= new Student("mary");
frank.geet()
output
Node.js Express Framework
What exactly is Express.js?
Express.js is a Node javascript web application server framework primarily built to create a single-page, multi-page, and hybrid web applications.
MEAN is a free and open-source JavaScript software framework for creating interactive websites and web applications with the following components:
  • MongoDB-NoSQL database standard.
  • Express.js-The default framework for web applications.
  • Angular.js-The JavaScript MVC framework is used to create web applications.
  • Node.js - It is a framework that is used for scalable server-side and networking applications.
Node.js and NoSQL database
NoSQL databases like MongoDB and MySQL have become very popular as data storage databases.
You need to download and use the necessary modules using the Node Package Manager to use any of these databases. The required module for MySQL is called "mysql" and the correct module to be installed for MongoDB is "Mongoose."

Written by mahesh-sharma | I am technical content developer & Blogger.
Published by HackerNoon on 2020/08/25