node date function

Creating Node.js Modules

Modules are javascript libraries that can be used in developing an application.They are set of functions.

Types of Modules

1.Built in modules-these are node.js modules that can be used without installing them.Example of these modules are:-

  • http-makes node.js act like http server.
  • https-makes node.js act like https server etc.

      How to include built in modules

To use a built-in module we use require () function adding the module name.

Example: var http=require(‘http’);

After adding above code now your application can access http module and be able to createServer

Example:


2.Own modules-Node.js developers can create their own modules and use them.

Example.

      Code to create a module that returns date and time

 

The exports keyword makes the module methods and properties available outside module file.

Save the above code as “firstmodule.js”

       Including your own modules

Example code.

Save above code as ‘demo_first_module.js’. Run the code by typing

C:\Users\Your Name>node demo_first_module.js

 

 

Note:

Code var date=require(‘./firstmodule’);  is used to include the module

 

We use  ‘ ./ ’ meaning that the module is contained in the same folder as the node.js file.

Output:

node date function

About the Author: Eric K

Kenyan Developer and Blogger with Interests in Java,Javascript,Node.js, Wordpress, Android, PHP and Making Money Online. Solving world problems through programming is my passion. Hire me: Email : kawi3462@gmail.com