Node Js Core Concepts

Posted on November 26, 2021 at 07:46 AM

NodeJs-Core-Concepts

Node Js Modules

Node Js Modules are set of code encapsulated in single or multiple files which can be use in project by require or import in variable and can be accessed throughout the project

Core modules

Those are inbuilt modules which can be used without any further package installation.

E.g. var http = require(‘http’); OR import { http } from ‘http’;

Third Party Modules

Those modules are which can be installed from different sources in the project to access the functionality of the code. Such code is available via NPM to install the package and can be accessed by require and import in the project.

E.g. var app = require(‘express’); OR import { express } from “express”;

Local Modules

Those modules are created in local projects which have some common functionality that can be used in different areas of the project instead of writing this functionality. every time in code we can created a local modules and can access by import and require

E.g.

mathModule.js

exports.addition = ( a , b ) => { return a + b }

Main.js

var math = require(‘./mathModule’);

var res = math.addition(2,5);

console.log( res ) // 7

Package.json

Package.json

It is an important file of the node js projects as it contains metadata of the project and it is always located in the root folder of the project.

package.json

{

“name”:”project-name”,

“version”:”0.0.1”,

“description”:”project description”,

“main”:”src/index.js”,

“scripts”:{

“Start”:”node index.js”,

“dev”:”nodemon”

},

“dependencies”:{

“express“: ”0.0.1”,

“compression”:”0.0.1”

},

“devDependencies”:{

“nodemon”:”0.0.1”

},

repository:{

“type”:”git”,

“url”:”git url”

},

“author”:”Nilesh Mandal”,

“contributors”:[

{

“name”,”contributor name”,

“Email”:”contributor email”,

“url”:””

}

],

“Keywords”:[“server” , “express”]

}

name: The name field defines the name of the package

version : It defines the version number and mostly use when project is published

description : It should have short information about the package which is used to get search results in npmjs.com. And also used for simple documentation of the project.

main : This defines the entry point of the project.

scripts : Scripts are powerful tools that npm cli use to run tasks for your project. It takes an objects with keys and values where keys are the scripts and values are the command. Example : npm run <scriptsName>

dependencies : All the project dependencies are listed here when a package is installed from npm cli. It keeps the object as the name of the package in key and version of the package as values. The package is downloaded in node_modules/package_name and entry is added in dependencies.

devDependencies : The package’s details are kept here which is used while development is needed.The details is kept in the same way as in dependencies and to add package as dev dependencies need to add –save-dev in cli command while installing package e.g npm install –save-dev <packageName>

repository : The field is an object where the keys are type and url as in type have to define which version control is used as git or bitbucket and url is the project codebase url.

author : The author details are mentioned here its values is as string <name> <email> <url>

contributors : It stores objects with contributors name , email , url as multiple contributors can contribute in the project so objects are stored in an array to list contributors. 

keywords : It is an array of strings the purpose is the same as to describe the project. This helps in npmjs.com for search results and each value is one keyword associated with packages installed.

keywords

Package-lock.json 

This file is used to keep track of version of each package is installed in the project

Non-Blocking I/O 

It means whenever the request is received by the server it triggers the operation and doesn’t wait for the response. During this time servers have enough free time which can be managed to get more requests from the client side.

Non-Blocking code refers to code that doesn’t block execution, Due to Asynchronous operation the big advantage is you can maximize the usage of memory and as well as single CPU.

Example code

const fs = require (‘fs’);

// file.txt : Hello Node.

fs.readFile(file.txt’, ‘utf8’, function(error, contents){

console.log(contents);

})

console.log(‘Hello Express.’); 

// file2.txt: Hello React

fs.readFile(‘file2.txt’,’utf8′, function(error, contents){

console.log(contents);

})

console.log(‘Hello Socket.’);

output : Hello Express  Hello Socket Hello Node Hello React

Global Objects 

Global-Objects

Global objects are objects that are available in all modules. Those objects are built-in objects in javascript which can be accessed without importing in any particular modules.

e.g. setTimeout() , setInterval() , console.

Event Loop 

It is a programming design pattern which provides a callback stack to the events that are linked to the resources and gets executed whenever an appropriate response is ready.

Steps to understand the process.

Timers

Timers or functions that execute code after a set period of time. The global functions setTimeout() and setInterval(). The timer phase is executed by the event loop at the beginning of this phase as it updates its own time and then checks a queue or timers or pool. As this queue consists of all timers are currently set. The Event loop takes the timer with the shortest wait time and compares it with Event Loop’s current time.If the wait has elapsed, then the timer’s callback is queued to be called once the call stack is empty.

I/O Callbacks 

The Synchronous I/O request is recorded into the queue and then the main call stack can continue working as expected. In the second phase of the Event Loop the I.O callbacks of completed or errored out I/O operations are processed.

Idle / waiting 

This is the idle phase where the event loop does nothing and prepares for the next phase.

I/O polling (poll phase)

In this phase it looks for the callbacks queued in the poll phase and it will execute them until all the callbacks drained up from the poll phase call back queue.

And if there are no callbacks in poll phase queue then it will stay in poll phase for some time for the new callbacks and move to setImmediate() to perform callback except for the setTimeout() as its time mentioned to perform callback. At this point it moves to the next phase.

setImmediate() callbacks

setImmediate phase is called when there are no callbacks in poll phase.In this phase all the setImmediate() callbacks will execute one by one till the queue gets empty.

Closing callback or close events

In this phase all the closing events callback executes such as process.exit()

Async && Await

Async keyword is used in function as it will wait for third party response as we mention before the function which promises to return some value because of this the dependent code will get some value to perform instead of getting thread break because of undefined value. 

Async is useful to hold code in particular functions to get responses as it is synchronous in nature.

E.g. 

async function functionName(){

return await getName();

}

Node Js Event Emitter

As in frontend javascript has lots of events to handle for the same in node js events can be created and handled on call.

Even initialize

const EventEmitter = require(‘events’)

const eventEmitter = new EventEmitter();

eventEmitter.on(‘start’ , (value) => {

console.log(‘Started’ , value);

})

eventEmitter.emit(‘start’ , 51);

Node Js FS Module 

To create , delete , read files in system node js provides built in modules fs. 

File system operations can be synchronous or asynchronous as per user requirements.

const fs = require(‘fs’);

fs.readFile(‘input.txt’ , (err,data) => {

if(err){ console.log(err) }

console.log(‘File reading’ ,data );

})

  • Read Files 
  • Write Files 
  • Delete Files 
  • Append Files 
  • Close File 

NodeJs Path Module

Node-Js-Path-Module

Path module is an inbuilt module which deals with path and directories.

const path = require(‘path’);

path.basename(); // return the last file name

path.dirname(); // return the directory name of the path

path.extname(); // returns the extension name of the file

path.join(); // It joins all the two path and normalise the path as well

NodeJs OS Module

This module provides information about the system operation system.

var os = require(‘os’);

os.userInfo() // returns the user information.

os.hostname() // returns the hostname of the system.

os.platform() // returns about the platform of the operating system.

os.freemem() // returns the numbers of free memory in the system.

os.type() // returns the type of operating system.

Node Js Http Module

Node-Js-Http-Module

Https module is inbuilt module of node js. Which allows to transfer data over hyper text transfer protocol (HTTP). Http create an http server that listens to server ports and gives a response back to the client

 // create http server

// main.js 

var http = require(‘http’)

http.createServer( (req,res)=>{

res.write(‘Hello World’);

res.end();

}).listen(8080);

// get request to get data into server 

// main.js

const https = require(‘https’);

const options = {

hostname:”dummy.restapiexample.com”,

port:443,

path:”api/v1/employeesz”,

method:”GET”

}

Const req = https.request(options , () => {

console.log(“status code”,res.statusCode)

res.on(‘data’ , d => {

process.stdout.write(d)

})

})

req.on(“error” , ()=>{

console.error(error)

})

req.end();

// post request 

// main.js

const https = require(‘https’)

const data = new TextEncoder().encode(

  JSON.stringify({

    todo: Task to do’

  })

)

const options = {

  hostname: ‘whatever.com’,

  port: 443,

  path: ‘/todos’,

  method: ‘POST’,

  headers: {

    ‘Content-Type’: ‘application/json’,

    ‘Content-Length’: data.length

  }

}

const req = https.request(options, res => {

  console.log(`statusCode: ${res.statusCode}`)

  res.on(‘data’, d => {

    process.stdout.write(d)

  })

})

req.on(‘error’, error => {

  console.error(error)

})

req.write(data)

req.end()

Node Js Buffers

Node-Js-Buffers

Buffer is a node js built in module and used to interact with streams of binary data.

It has a storage spot for chunks of data that has been transferred from one place to another place. Buffer is filled with data that passed along. It transfers small chunks of data at a time.

E.g.

// create a empty buffer

const  buf1 = Buffer.alloc(10);

// create a buffer with content 

const buf2 = Buffer.alloc(‘Hello World’);

console.log( buf2.toJson() );

console.log( buf2.toString() );

Node Js Streams

Streams are built in node js modules which are used to handle reading/writing files, network communications, or any kind of end-to-end information exchange in an efficient way.

As it mentioned above it is used to transfer data and it improves performance as well. By using streams you read it piece by piece, processing its content without keeping it all in memory.

Writable streams : Allow node js to write data to a stream 

Readable streams : Allow node js to read data from a stream

Duplex streams : Can read and write to a streams

E.g. 

const http = require(‘http’)

const fs = require(‘fs’)

const server = http.createServer((req, res) => {

  const stream = fs.createReadStream(__dirname + ‘/data.txt’)

  stream.pipe(res)

})

server.listen(3000);

In this example pipe() method is called on the file stream. Instead of waiting until the file is fully read, we start streaming it to the HTTP client as soon as we have a chunk of data ready to be sent.

Related Posts

Start a Project

We could talk tech all day. But we’d like to do things too,
like everything we’ve been promising out here.