Lesson 3 Chat

Today we'll do a little more on FS module and then move on to
External Node Modules

Are you both here???

Ok... lets begin...

So where exactly to begin..

In the last session we saw how to read a file using FS,
today we'll do how to:

Read, Write, Delete those files ...

Lets start..

console.log are used usually to show messages or information...

console.error(); specially used for showing errors.. of course we can also use console.log for errors but that's not the standard way...

These two lines are enough to write to a file. The first argument is the filename and the second one is the content of the file we want to write..

Let's run it...

Note that right now, we have only one file in the "Lesson 3" folder...

After writing, there'll be one more file with the name anything.txt

NOTE:
In Windows, the command is
"node filename"
In Linux, it is:
"nodejs filename"

Good question renu!
the anything.txt file got auto-created....

Let's try to delete it now..

or wait... maybe read it first, and then delete it..

Note that in delete function (which we call "unlink"), there's no argument called "data". There is only "err". This is because we don't want to use the data. We just want to delete that. And if we get some error while deleting the data then we use the error argument...

Okay?
Alright...
kkk
Now, notice very carefully as I run the command... The left sidebar...
"anything.txt" file will vanish...
That's what "unlink" (delete) function does...

That's the basics of FS module. You're now capable of doing file handling in Node.js... congrats!

Let's move on to the next topic...

------- CLOSURE ------------------

A closure is....well...nothing but a function inside another function. It's same as inner classes in Java, but since it JavaScript doesn't support classes natively, we use functions and prototypes to meet the requirements...

We'll learn about prototypes soon..

Let's take a quick e.g. at closure...

The app wont run because we havent passed any argument to the function yet... :D

Still the app wont show anything because the inner function wasn't called yet :D

Console.log is inside the inner function, see?

Damn... we broke something... xD
Let me fix it..

Phew!
hopefully it works now...

Something's wrong...


yeah lets continue for now...

There are two popular functions called setInterval and setTimeout, which are heavily used in websites... I used it many times too...

Let's first look at

------ setInterval -------------

This is a "immediate function", we'll see later that immediate function actually does... but for now, it is a function that calls itself without any external call, just like a recursive function, except that it has a time limit set to it...

Syntax:

setInterval(function, timelimit);

Right now, I didn't pass any time limite, and therefore the function is being called every milisecond... The moment I give the command, it runs continously every milisecond... You'll see thousands of 'Heello there!' printed ...

Nope, there's no limit. It'll keep printing until your laptop battery is down....

But, you can set a time interval, which species at what intervals of time it should call again and again...

like this.

Did you notice?
The time is set in milliseconds...
To break the process, you need to press Ctrl + C inside the terminal window.

Can you guess the output?

Exactly, this is because the function is inside the setInterval function, which means that the 'x' variable here is a local variable, it is created and destroyed, every time the function is called, even if you increment or make changes to it. DOn't think that the x will change to 1, 2, 3 ,4....etc with each iteration.
Nope, x++ won't work here a..


Let's do a similar function..

it's called setTimeout

By the way... in the weather app that I made, I've used the setInterval function to update the weather information every millisecond, so that it appears realtime :)

setTimeout: It triggers a function only ONCE after a time limit. The 'Hello!' was printed exactly after 2 seconds in this example from the time of running it.


Ok, got it what you meant... you mean..
you'll have to declare the variable globally in that case...
Should I show?
Simple, right?


Let's move on...

wait.. move on with what exactly :/

Lets do a quick recap first..

There are 3 types of modules:
1) File Based Modules (where we use modules.export to use our own modules)
2) Node Modules: like the FS, OS, modules which we saw
3) External Node Modules...


Now we'll look at External Node Modules.
Important:::
This is what makes Node.js huge and super powerful.

There are unlimited external modules!!!

That is because every minute, a new module is added in the Node repository online.

By the way... i have around 200 apples now.... LOL!

-------- EXTERNAL NODE MODULES -----

npmjs.com

is the home of all the external node modules, createdd by Node.js developers themselves...

TO THE DEVELOPERs,
FOR THE DEVELOPERs,
BY THE DEVELOPERs.

hehe...

ok,  enough with the Ads...
lets start using them...

Do this to search for a module..

Underscore is a very popular module used for searching, sorting, doing quick calculations, data manipulation, etc...

"npm install underscore" is used to install the module on your computer.

"npm install moduleName" is the general syntax to install any module...
You also need to use
"sudo apt-get install npm" beforehand to be able to use NPM modules.
NPM: Node Package Manager

sudo: Super User Do
Same as Admin in Windows

Where's Renu?
she got disconnected again..
Lets move on, she'll have the notes...


To import a "underscore" module, we use _ symbol.


Suppose you have a database of students who have submitted their college fees.

I want to impose fine on students whose submitted fees are less than 60,000.
Instead of doing manual headache...it is easier to use module and save time!

We have a results array with values greater than 20 stored...

item is a variable which scans for every element in an array. It is like saying, it checks for
myArray[0],
myArray[1],
myArray[2],
myArray[3],...
whenever it finds anything more than 20, it returns the value and stores in results variable...

by the way... we never declared or said that "results" is an Array. But, since, we are returning values FROM an array to a variable, Node.js is smart enough to think of "results" as an array itself...
We get the result in array from..

Is this much clear?
Ok... lets do one more underscore example...

Let's get all the even numbers from this array: [1,2,3,4,5,6,7,8,9];

Result should be 2,4,6,8

Is it clear?

I just used the "reject" and the "filter" functions... there are many more functions available in "underscore" module...

They can be found at:
underscorejs.org


Comments

Popular posts from this blog

Lesson 2 Chat

Arrays