[Episode two] NodeJS Modules for web development

What is a Module in Node.js?

Consider modules to be the same as JavaScript libraries. A set of functions you want to include in your application that would help perform some special functions too.

Built-in Modules

Node.js has a set of built-in modules that you can use without any further installation.

Include Modules

To include a module, use the require() function with the name of the module:

var http = require('http');

Now your application has access to the HTTP module, and is able to create a server:

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello World!');
}).listen(8080);

Create Your Own Modules

You can create your own modules, and easily include them in your applications.
The following example creates a module that returns a date and time object:

Example

Create a module that returns the current date and time:

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

Use the exports keyword to make properties and methods available outside the module file.

Save the code above in a file called "myfirstmodule.js"

Include Your Own Module

Now you can include and use the module in any of your Node.js files.

Example

Use the module “myfirstmodule” in a Node.js file:

var http = require('http');
var dt = require('./myfirstmodule');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write("The date and time are currently: " + dt.myDateTime());
  res.end();
}).listen(8080);
Notice that we use ./ to locate the module, which means that the module is located in the same folder as the Node.js file.

Save the code above in a file called “demo_module.js”, and initiate the file:

Initiate demo_module.js:
C:\Users\Your Name>node demo_module.js

If you have followed the same steps on your computer, you will see the same result as the example: http://localhost:8080

John Danielhttps://firmwarefile.co
Android biggest Fan and a Tech Nerd

Related Articles

[Flash File] Realme C15 RMX2186 Firmware Download [Stock Rom]

In this article, we are going to provide you with all the Realme C15 RMX2186 firmware flash file ready for download. Also, we are going...

[Flash File] Realme C11 RMX2185 Firmware Download [Stock Rom]

In this article, we are going to provide you with all the Realme C11 RMX2185 firmware flash file ready for download. Also, we are going...

[Flash File] Realme C11 RMX2184 Firmware Download [Stock Rom]

In this article, we are going to provide you with all the Realme C11 RMX2184 firmware flash file ready for download. Also, we are going...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

How Has Technology Transformed The Healthcare Sector?

The world is fast evolving owing to the astounding rate at which technology has been growing in recent years. The growth in technology has...

Ways In Which Technology Has Changed Lifestyle of People

In the last few years, the advent of technology has shaped human lives in previously unfathomable ways. It has not only offered tools of...

Pro tips to manage freelance web designing projects

Being a Freelance web designer has its own advantages and so the challenges. As here, you are not only pursuing web designing, but you are building...

Common Mobile Phone Problems With Solution

Just as human beings cannot be perfect, a mobile phone cannot be. There are a lot of times when your mobile phone starts facing...

7 Steps to Keeping Your Employees Safe in the Workplace

Workplace safety should always be one of the top priorities for any business owner, manager, foreman, or HR professional – especially if they work...