Learn Node.js

Node.js Module

Node.js module is like JavaScript libraries.Node.js Module is like set of function that you can to include in your application.

Built-in Node.js Modules

Node.js comes with following built in module,Which you can use without any further installation.

How to Include Node.js Modules

To inculde Node.js module, use the require() function with the name of the module as given below.

var http = require('http');

Create Your Own Node.js Modules

You can create your own Node.js module and use them in your web application.

In Below example we will create an module that returns a date and time object.

exports.myDateTimeModule = function () { return Date(); };

Note: exports keyword is used to make methods and properties available outside the module file. Save the above code in a file called "myfirstnodemodule.js"

Include Your Own Module

Use the module "myfirstnodemodule" in a main.js file like below example.

var http = require('http'); var dt = require('./myfirstnodemodule'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write("Current Date and Time is: " + dt.myDateTimeModule()); res.end(); }).listen(4004);

"./" means New module (myfirstnodemodule) is located in the same folder as the main.js file

Run the Node.js Program

Go to location where Node.js file has been save.

C:/Users/Your Name>node main.js

Now open browser, and type the following in browser.

http://localhost:4004
Module Descritpion
assertProvides a set of assertion tests
bufferTo handle binary data
child_processTo run a child process
clusterTo split a single Node process into multiple processes
cryptoTo handle OpenSSL cryptographic functions
dgramProvides implementation of UDP datagram sockets
dnsTo do DNS lookups and name resolution functions
eventsTo handle events
fsTo handle the file system
httpTo make Node.js act as an HTTP server
httpsTo make Node.js act as an HTTPS server.
netTo create servers and clients
osProvides information about the operation system
pathTo handle file paths
punycodeDeprecated. A character encoding scheme
querystringTo handle URL query strings
readlineTo handle readable streams one line at the time
streamTo handle streaming data
string_decoderTo decode buffer objects into strings
timersTo execute a function after a given number of milliseconds
ttyProvides classes used by a text terminal
urlTo parse URL strings
utilTo access utility functions
zlibTo compress or decompress files