Friday, March 17, 2023
HomeNodejsNodeJS Web Module - How one can Create Servers and Shoppers in...

NodeJS Web Module – How one can Create Servers and Shoppers in NodeJS


NodeJS Web is a built-in module used to create servers and purchasers. It may be utilized in various kinds of purposes to create a connection between servers and purchasers, it acts as a bridge of communication between servers and purchasers. Beside this Node.js Web offers completely different strategies used to validate the IP tackle, to examine the IP tackle sort i.e IPv4 or IPv6.

For a greater understanding, let’s think about an instance, suppose we have now to create a gaggle chat utility, and we have now to create completely different teams for various customers, right here the group is taken into account a server and customers are purchasers linked to completely different teams, so we are able to use Node.js Web module to create completely different servers on a distinct port and provides customers the choice to decide on which server they need to join, and as per the selection, create a shopper utilizing the Web module of that server which use to attach with the server. 

Additionally learn: NodeJS Timer Module and Strategies to Know

Node.js Web Module

For utilizing Node.js Web functionalities, it’s required to import Web Module utilizing the next code.

Syntax:

const internet = require('internet');

Create Server utilizing Node.js Web Module

Node.js Web offers a way used to create a brand new server.

Syntax:

Instance:

Let’s create a server and set it to a relentless then use that fixed and create a port utilizing the pay attention methodology to take heed to the server, right here we specified the port as 3000, and print a string to verify whether or not the server is efficiently created or not.

const internet = require('internet');

const server = internet.createServer();

server.pay attention(3000, () => {  
  console.log('Server listening on port 3000!');  
});  

Output:

Create Server Example

Create Shopper utilizing Node.js Web Module

There are two strategies supplied by Web Module to create a shopper, the primary is join and the second is createConnection, however each strategies are utterly similar, they’ve the identical definition.

join

Syntax:

internet.join(port, callback); 

Instance:

Let’s use the join methodology to attach with the server we have now created within the above step.

const internet = require('internet');

const server = internet.createServer();

server.pay attention(3000, () => {  
  console.log('Server listening on port 3000!');  
});  
 
internet.join(3000, () => {
  console.log('linked to server!');  
}); 

Output:

Connect Example

createConnection

Syntax:

internet.createConnectiont(port, callback); 

Instance:

To see the distinction between join methodology and createConnection methodology, use the identical instance by simply altering the key phrase connect with createConnection.

const internet = require('internet');

const server = internet.createServer();

server.pay attention(3000, () => {  
  console.log('Server listening on port 3000!');  
});  

internet.createConnection(3000, () => {
    console.log('linked to server!');  
});  

Output:

Create Connection Example

Strategies of the Node.js Web module

There are strategies used for IP tackle validation, it detects whether or not the entered IP tackle is appropriate or not, whether or not it’s an IPv4 or IPv5.

isIP

This methodology takes a string as an argument and checks whether or not it’s a legitimate IP tackle or not, it isn’t a sound IP tackle then the strategy returns 0 as output.

Syntax:

Instance:

const internet = require('internet');  

const end result = internet.isIP("8.8.8.8");

if(end result) {
    console.log("It is an IP tackle");
} else {
    console.log("It is not an IP tackle");
}

Output:

IsIP Example

isIPv4

This methodology takes a string as an argument and checks whether or not the entered IP tackle is an IPv4 or not. IPv4 is developed within the 90s and has 4 billons addresses utilized by nearly all of web sites. This methodology returns a real if the argument handed is an IPv4, and returns a false if it isn’t IPv4.

Syntax:

Instance:

const internet = require('internet');

const end result = internet.isIPv4("8.8.8.8");

if (end result) {
    console.log("It is an IPv4 tackle");
} else {
    console.log("It is not an IPv4 tackle");
}

Output:

IsIPv4 Example

isIPv6

This methodology works the identical because the isIPv4 methodology however as a substitute of checking for the IPv4, it validates whether or not the entered IP tackle is IPv6 or not. The variety of IPv4 addresses is restricted that why IPv6 is created which is utilized by new web sites.

Syntax:

Instance:

const internet = require('internet');  

const end result = internet.isIPv6("8.8.8.8");

if(end result) {
    console.log("It is an IPv6 tackle");
} else {
    console.log("It is not an IPv6 tackle");
}

Output:

IsIPv6 Example

Abstract

Node.js Web module is used to create servers and purchasers for that server, it’s primarily utilized in chat purposes the place we normally should create many servers and purchasers which connect with share data, Node.js Web will create that connection, it may well additionally use for checking the IP tackle varieties and to validate a string that it’s an IP tackle or not. Hope this text lets you perceive the Node.js Web module.

Reference

https://nodejs.org/api/internet.html#internet

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments