How to Import Custom Module in Node JS


How to Import Custom Module in Node JS
Custom Module in Node JS

Hi friends, in this tutorial, you will learn how to import a custom module in Node JS. Before getting started with the custom module, you must know what the module means in node js and how to include that module before creating any object. A module is nothing but a javascript function or a library.

Also, there are built-in modules and you can directly invoke those modules with the help of require() function by assigning them to a javascript variable. As I already discussed one of the built-in modules (http) and how to use it. So, I will explain the custom module in a step-by-step process.

Also read, the Node JS HTTP server example

Three steps to import custom module in Node JS

  • Create a javascript file where the main node js file is located.
  • Declare a function or more than one function in that javascript file.
  • Invoke that external JS file in the main node JS file using the require() function as shown below.

var random = require('./random.js');

Please note that random.js is assigned to a variable ‘random‘. Later on, we will use this random variable to call the functions executed in the random.js file.

random.js:-

exports.getRandom = function(){
       return Math.floor((Math.random()*10)+1);
};

In the random.js file, there is a function called getRandom() which returns a whole random number between 1 to 10 every time you initiate the main node js file in your browser.

main.js:-

var http = require('http');
var random = require('./random.js');

http.createServer(function (req, res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write("This is the random number every time you hit the browser.       "+random.getRandom());
    res.end();
}).listen(8080);

In the above file, you have noticed that I have included two modules inside the require() function. One is built-in module (http) and the other is random.js.

  • http module is used to transfer data over the hypertext transfer protocol which means over the web browser.
  • random.js is used to print random numbers.
  • createServer() object is created using the http so that the response is returned from the server.
  • When we call the getRandom() function using the random variable inside the server object, we will get the random numbers from the server each time the request is sent from the browser.

If you hit https://localhost:8000 from your browser, you will see the below output

This is the random number every time you hit the browser. 3

Conclusion:- I wish this tutorial will help you to understand the concept of importing a custom module. If there is any doubt then please leave a comment below. You can also download Node JS from here.


Leave a Comment

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

KUPU178

KUPU178

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

KUPU178

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

KUPU178

daftar ceri188

cery188

ceri188

KUPU178

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

HOKI311

THOR311

RATU311

KUPU178

THOR311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

KUPU178

THOR311

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

KUPU178

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

THOR311

RATU311

KUPU178

KUPU178

RATU311

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

KUPU178

THOR311

HOKI311

KUPU178

RATU311

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311