We have seen the Nodejs Tutorial steps in the previous blog. Now lets understand the main and commonly used components and the modules in the Node.js and its usage in the Node.js application.
Node.js Components
Node.js uses the below given components:
- Import required modules: Node.js application use the require directive to load the node.js modules
var http = require(“http”);
- Create Server: The server which listens to the node.js application request
- Request & Response: The node.js server will intercept the requests from the browser / console and returns the response back to the client application.
Refer the below program which makes the use of these components
var http = require(‘http’);
var requestListener = function (req, res) {
res.writeHead(200);
res.end(‘Hello, World!’);
}
var server = http.createServer(requestListener);
server.listen(8080, function() { console.log(“Listening on port 8080”)})
Node.js modules
Node.js is built on JavaScript framework and creates .js files for processing. Node.js modules provides the functionality to add the encapsulation logic for segregating the functionality into different modules. Node.js provides the below available modules :
- Express Framework: The provides the features to develop robust web and mobile applications
- MongoDB: provides support for the MondoDB and Node.js integration for database operations
- Restify: Framework to support REST APIs integration
- Jade: provides high performance template engine for nodes and browsers
- Bluebird: provides promise library with full of feature and performance enhancements
- Socket.io: provides real -time bi-directional event based communication, mpostly use for developing chat application
Node.js build modules
Node.js provides the in-built modules for processing the java script application.
Module Name | Module Description |
assert | Provides a set of assertion tests |
buffer | handles binary data |
child_process | for running the child processes |
cluster | manages multiple node processing |
crypto | manages OpenSSL cryptographic functions |
dgram | manages UDP datagram sockets |
dns | manages DNS Lookup and name resolution functions |
domain | deprecated now. |
events | manages events |
fs | manages file system |
http | manages node.js to run as HTTP Server |
https | manages node.js to run as HTTPS Server |
net | helps in creating server & clients |
os | provides operating system data |
path | manages file path |
punycode | deprecated now |
querystring | manages URL query strings |
readline | manages readable streams one line at the time |
stream | manages streaming data |
string_decoder | decodes buffer objects to strings |
timers | execute a function after a given number of milliseconds |
tls | manages TLS and SSL protocols |
tty | Provides classes used by a text terminal |
url | parse URL strings |
util | provides utility functions |
v8 | accesses information about V8 (the JavaScript engine) |
vm | compiles JavaScript code in a virtual machine |
zlib | compress or decompress files |