Welcome to FindNerd. Today we are going to discuss in-build modules in nodejs. If you work in nodejs then you should be familiar with modules. There are many in-build modules available in core libraries of nodejs. You can also get the modules from npm. npm stands for Node package manager where you can get the modules which are developed by thousands of developers. You can also build your custom modules and can load it in your application. You can load the module using require function by passing the name or path of the module.
require('os'); // loaded the os inbuild module
require('./train'); // loaded custom module
Here is the list of the modules available in core library of nodejs. Please have a look.
http : It enables the features of HTTP protocol. It includes different classes with different methods such as http.Agent, http.ClientRequest, http.Server, http.ServerResponse, http.IncomingMessages. These classes are used to handle HTTP Server and Client.
https : This is same module like http but it uses HTTP protocol over SSL/TLS.
Cluster : We know that nodejs works on single thread. If you want to work on multi core-systems then you can load the cluster of nodejs.
Crypto : Enable the cryptographic functionality and use the OpenSSL's hash, HMAC, cipher, decipher, sign and verify functions.
fs : fs stands for file system and enable the file I/O over standard POSIX functions.
util : Supports internal APIs of nodejs.
tls : Enable the implementation of transport layer security and Secure socket layer protocols.
url : Enable URL resolution and parsing.
vm : vm stands for Executing javascript. It enables APIs for compiling and running code within V8 virtual machine.
zlib : Enable Gzip and Deflate/Inflate for compression.
tty : It includes tty.readStream and tty.writeStream classes. tty is nothing but terminal or command prompt and a way to fix the things in computer.
string_decoder : Enable API for decoding Buffer objects into strings using encoded multi-byte UTF-8 and UTF-16.
repl : Enables a Read-Eval-Print-Loop (REPL) implementation.
readline : Enable reading from Readable stream and can read one line at a time.
punycode : Punycode enables converter that fully complies to RFC 3492 and RFC 5891.
path : Enable methods to handle the files and directories paths.
querystring : Enables parsing and formatting URL query strings.
os : Provides methods related to operating systems.
net : Enable asynchronous network wrapper and functionality to create the client and server both.
events : Enable events management.
dns : Using operating system facilities to perform name resolution as well as deals with the actual DNS to perform name resolution.
dgram : Enable UDP Datagram sockets implementation.
child_process : Enable the ability to spawn child processes.
buffer : enable the interaction with octet streams related to TCP streams and file system operations.
assert : Includes simple set of assertion tests and use for internal only.
Above mentioned in-build modules include different classes and methods which can perform different operations. We are going take small examples to explain the working.
// example with http module
// file name serverhttp.js
var http_mod = require('http');
var request_mode = function(req,res){
res.writeHead(200,{"Content-Type":"text/plain"});
res.end('You are Nerd');
}
var server_mode = http_mod.createServer(request_mode);
server_mode.listen(3000,'localhost');
In above example we have loaded the http module and create a request for server using createServer function and listening to port 3000. You need to open the terminal and write the command.
node serverhttp.js
It will start the server and you can write http:://localhost:3000, it will displayed the message on browser.
// other example with os module
// filename osmod.jd
var os = require('os');
console.log("dfd: " + os.type());
In above example we have loaded the os module and displaying message on console. you can run this program using this command.
node osmod.js
Thank you for being with us!
0 Comment(s)