Quick Contact


    Creating and loading Node Module

    Start creating module to this application by adding new module to the application. So create new file logger.js. Let’s create a module for login messages and the advantage of creating modules is you can reuse the module in various parts of this application or potentially in other application. In this module there will be use of remote logging services for login our messages. So, there are websites which provide logging as a service, they give us URL and you can send an HTTP request to that URL to log messages in the cloud. So, it is as-:

    Varurl= ‘’ //enter url here

    In this implementation, HTTP request is send to this endpoint -to this URL.

    Now you also need a function called log that takes a message and in this function HTTP request is send as-:

    Function log (message) {

    //send an HTTP request

    }

    So for now in these topics focus is on modularity instead of details of sending HTTP requests.
    So, now you need to log the message on the console as-:

    Console.log (message);

    Now that you have taken variable and log function, they are both scoped to this respective module. They are private; they are not visible from the outside. In logger module, you need to access the log function so that you will be able to call it from the app module. So, for this you need to make log function public and should make it visible from the outside.

    To make log function or to give access to anything outside, export function is used as-:

    Module.exports.log = log;

    So here log function is exported and after equal to ‘=’ sign it’s simply setting it to this log function you had defined here.

    Or you can say the object you are exporting has a single method called log. If you want to export the URL then it is as-:

    Module.exports.url = url;

    You can also change the name of url while exporting. For example- url to endpoint.

    You don’t need to export this URL variable because this is purely implementation detail.

    So, in real world application every module might have several variables and functions. You only want to export a subset of these members to the outside.

    Let’s take example of DVD Player
    Creating and loading Node Module
    As it has few buttons on outside and these are the buttons or objects that you used to interact. So, these objects represent the public interface of a DVD player.
    Creating and loading Node Module

    But inside the box there are lots of other objects or complex objects you don’t need to know anything about these objects and their implementation data and they can change significantly from one model to another. But what you see outside is almost stable or static across different models. So, the URL in logger module is implementation details. Module don’t need to know anything about this, they only need to call the log function so export these i.e. log public but keep the URL private so the final code is-:

    	Varurl = ‘http://.....’;
           Function log (message) {
          //send an HTTP request
        Console.log (message);
        }
       	Module.exports.log = log;
    	
    Now need to load the module and use it inside another main file i.e. “app.js” as-:

    Console.log (module);

    Copyright 1999- Ducat Creative, All rights reserved.