Nodejs Course

In Node.js you use modules to build your script. A module is the same as a JavaScript library, which can be reused throughout the Node.js application.
Node.js has a set of built-in modules (named also "core modules") which you can use without any further installation.
- See this page for a List with Node.js Core Modules.

Include Modules

To use Node.js module, you first need to include it using require() function.
var module = require('module_name');
Example: Using the HTTP module to create a Node.js server:
const http = require('http');

const server = http.createServer((req, res)=> {
 res.writeHead(200, {'Content-Type':'text/plain'});
 res.end('Hello to me.');
});

server.listen(8080, ()=> {
 console.log('Server running at //localhost:8080/');
});

Create Modules

You can create your own modules, and easily include them in your applications.
In Node.js, module should be placed in a separate JavaScript file.
- The following example creates a module that contains an object with properties for current date and time:
//module to get and use date and time
class mDateTime {
 //set propertie with date and time
 constructor() {
 this.dt = new Date();
 this.year = this.dt.getFullYear();
 this.month = this.dt.getMonth()+1;
 this.day = this.dt.getDate();
 this.hour = this.dt.getHours();
 this.minute = this.dt.getMinutes();
 this.seconds = this.dt.getSeconds();
 }

 //returns string with: year.month.day
 get date(){
 return this.year+'.'+this.month+'.'+this.day;
 }

 //returns string with: hour:minute:seconds
 get time(){
 return this.hour+':'+this.minute+':'+this.seconds;
 }
}

//assign objhecy of mDateTime class to module.exports
module.exports = new mDateTime();
Assign the desired object (here new mDateTime()) to module.exports to make properties and methods available outside the module file. So, the object can be used in the script when the module is included with require().
Save the code above in a file called 'mdatetime.js'.

- Now, we can use this module in a Node.js script.

For test, lets create a Node.js file that displays in browser the date and time when the server was started.
Copy and save the folowing code in a file called "demo_mdatetime.js", in the same folder as the "mdatetime.js" module.
//include the http module
const http = require('http');

//include mdatetime module; located in the same folder
var dt = require('./mdatetime');

//set a string with the date and time when the server is started, using the properties defined in the mdatetime module
var current_dt = dt.date +' - '+ dt.time;

//define constant for port
const port =8080;

//sets the server
const server = http.createServer((req, res)=> {
 res.writeHead(200, {'Content-Type':'text/plain'});
 res.write('Server started in the date-time: '+ current_dt);
 res.end();
});

//pass the port to server to listen to
server.listen(port, ()=> {
 console.log('Server running at //localhost:'+ port +'/');
});
- We use "./" to locate the module, that means that the module is located in the same folder as the Node.js file.
Initiate the file in the Command line interface.
If you don't know how to initiate a Node.js file in command line interface to start the server, see the previous tutorial: Node.js Get Started.
- I have the module and the "demo_mdatetime.js" file saved in "E:/nodejs/test/" folder, and I use this in command line interface to start Node.js with that file:
E:/nodejs>node test/demo_mdatetime.js
To see the results, open the server in your browser: //localhost:8080/
If you have followed the same steps on your computer, you will see the same result as in this screenshoot:
Node.js demo module mdatetime

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Node.js Modules

Last accessed pages

  1. JavaScript Course - Free lessons (31411)
  2. Properties and Methods of the Form elements (594)
  3. PHP Strings (3263)
  4. Selection Tools (8145)
  5. Get Lower, Higher, and Closest Number (5342)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide