AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js Modules


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js Modules

What are node.js modules?

In Node.js, modules are blocks of encapsulated code that communicate with an external application based on their functionality.
Modules can be a collection of multiple files / folders or a single file.
The reason why programmers rely heavily on modules is because of their reusability, as well as their ability to break down a complex piece of code into easy-to-manage pieces.

Built-in Modules
Node.js has a set of built-in modules which you can use without any further installation.  These modules can be loaded into the program by using the require function.

var module = require('module_name');

The require() function will return a JavaScript type depending on what the particular module returns.

 

Include Modules Example

The following example demonstrates how to use the Node.js Http module to create a web server.

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Welcome to Nodejs page!');
}).listen(1443);

In the above example, the require() function returns an object because the Http module returns its functionality as an object.
The function http.createServer() method will be executed when someone tries to access the computer on port 1443.

 

Create Your Own Modules

You can create your own node.js modules and easily include them in the applications you need.
In the following example, we created a module that returns a date and time object:

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

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

 

Include Your Own Module

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

Example: Use "my_first_module" module 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);DROP VIEW [France Customers];
Note: we use ./ to locate the module, that means that the module is located in the same folder as the Node.js file.

 



node.js modules, list, example, install, can be exposed using, documentation, module exports, explained, path, tutorial, can be exported using
What are some popular node js modules?
What are node modules used for?
What are js modules?
What are the CommonJS modules?
Node.js Modules - nodejs

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...