Lesson 2 Chat

Today we'll do ...  well at least these topics:

1) Functions .......................... DONE
2) File Based Modules
3) Internal Modules
4) External Node Modules

First of all, what is a module?

A module is like a .... header file in C/C++, in Node.js.
A module can have multiple functions, states, properties, definations and a lot more...

Before we dive into modules, lets do some basics first (this will help us in understanding modules better)...

------------------------------- OBJECTS ----------------------------

An object in Node.js has some "key" and "value" pairs. It is very different from objects in C++/Java (where an object is basically an implementation of class....where we say class is a blueprint of an object).
Here it is a bit different because the objects here don't follow all the OOPs properties...

Lets do a quick example

So, that is how you declare an object.

We just declared an object in Node.
It is  really simple...
We can access the object values like this:
lets run it...

An object can not only have values, but also some functions... but before that, we need to know how to use functions.

Lets make a function.

What we did here is, created a function named "mySum",
we passed two parameters named x and y to it.
Note: We didnt declare var x and var y here. Function parameters
in Node don't require declaration, although you can if you want to. Then we made  a new variable z and stored the sum of x and y inside z.
Then we simply print the sum...

Lets see 2nd method of declaring a function...

() => {
}

This format is called an Arrow Function...

Is it clear?
???
Ok...

There are only minor syntactical differences here...

NOTE: The arrow function cannot allow the "this" operator. Other two functions can.

Sum of ' + x + ' and ' + y + ' is ' + z
In this line, we are just showing the actual values of x and y, which were passed by the function call...

What i just told about "this" operator, we'll learn that while learning "Prototypes"..

Now when we know how to use functions, lets add them to Objects.


------------- FILE BASED MODULES -------------------------

We already know what a module is.
Now we'll do the first type of module....

Every ".js" file is a module itself.

We can use the functions present inside one module, into another .js file. To do that, you needd to export and import those functions.... lets see that  now...

It means that we can now export and use these functions in other files...
LEts create a demo file...

We created a variable named "myfunction" (should be same as the filename from which we are importing the functions) and used "require" to specify the name.

NOTE: In File Based Modules, we use './filename' syntax. It is different in other types of modules...

You see we get the function result in this file...

"require" is the name to import a file.
Just like "include" in #include<conio.h>


We are importing in demo.js
This was all about File Based Modules...

Now, lets do Node Modules.
These are the predefined modules present in Node Core Engine, just like conio, stdio, assert, etc in C/C++...


------------------- NODE MODULES --------------------------

All the Node Modules can be found here:
https://nodejs.org/dist/latest-v8.x/docs/api/
In the above link, you get all the list of available modules and, how to use them...

Let's try using 'OS':

1) OS Module
Note: When I use a file based module, I don't use the dot operator here...
In File Based Module, its like:
var filename = require('./filename');

but in Node modules, it is like:
var filename = require('filename');

Remember the difference.

1) The OS Module - very popular
OS Module helps in determining various OS related functions and information, like the available RAM, disk storage etc.
This is needed, because when you're running a website and using a web server, your wesite should load in the most optimized way in the user's computer....

os.totalmem(), freemem(), etc are the inbuilt functions in the OS module that helps in doing so....

I just made a variable named gigabyte so that we can get the results in GBs. instead of many KB's (which they return by default)... I got that by dividing the x KBs three times in the power of 3.
like x multiplied by y muliplied by z = abc KBs
GBs = abc / 1024*1024*1024...

Let's see the number of CPUs present using OS module...

There are more OS functions, can be found here:
There's really a lot...

2) File System Module

IT's another popular module. It is used for creating, reading, deleting, renaming filenames in the server. I used this in my Weather App too...

Let's try to read a text file using this module...
We first create a text file named ..... anything.txt
Now, we'll read the data from this text file using the "fs" module

readFile() function has two parameters, the first one for specifying the file name, and the second one for doing the actualy file manipulation...
Note: The second parameter is a function itself. This function is called a "Callback Function" -- this is super important in Node. because one of the powerful features of Node.js is it's ability to call a whole function as a parameter after the first parameter is successful. Here, the first paramter is to read the file "anything.txt"...
The "err" will show an error if the file is missing or something goes wrong...
Good question.
err and data are both predefined parameters in readFile function. They are compulsary for reading the file.

Comments

Popular posts from this blog

Arrays