NodeJS Series #6: Event-Driven Programming

Standard

Hi Wizards,

Its been some time since I’ve left Hogwarts Magicians School for a small holiday.Now that Im back,im ready for some tricks.

But before that,i’d like to talk about Event-Driven Programming and in its place in NodeJS

What is Event-Driven Programming?

Event-driven programming is the term where the flow of the code is determined by events(click,load,etc…)
Its one of the basic milestone today’s popular programming languages such as C#,Java and many more I dont wanna fill up here.
In Node.js and moreover in any kind of Javascript project,you’ll be using or used event-driven process.Whether its page onload or button click event,this is something you have achieved with our without knowing.

Lets make an example to the classic event-driven process and how its done in NodeJS:

result = getJSONfromDestination();
binddata(result);

The operation above requires blocking I/O process (single thread operation that waits for previously working synchronous code )

Now lets take a look at how we do asynchronous way to do it(Non-blocking I/O process)

json_finished = function(result){
binddata(result);
}
getJSONfromDestination(jsonfinished);

As you can see,this is non blocking sample,because json_finished doesnt work first as you can imagine.
It starts working when you call getJSONfromDestination method and sending param as function to json_finished.

This is how Asynchronous operations are completed in mainly Javascript projects and widely used in NodeJS projects.In case of working step-by-step,we send params to functions to start working whenever & however we want.

You’ll be using frequently these kind of Asynchronous operations or in other terms “Event-Driven Programming” style in almost all of your JS oriented applications,not to mention coding like that is a “must-have” in NodeJS applications.

Next article,we will be experiencing a real world problem for detailed workaround.

Advertisements

NodeJS Series #5: Installing,Uninstalling,Updating Modules

Standard

Hi Wizards,

Do you remember what we did previously? We created a Port Listener which listens to a port and get response.
In that post, we used a module named “http” to send request and get response.

Now im gonna talk about “Modules” and how to use them.

MODULES

Node Package Manager consists of many modules that can be downloaded and used in your project.The list of repository can be accessed from: https://www.npmjs.com/

You can however specify if a module will be used local or global.

What does that mean?

Global means it can be used in other projects as well.But by default,Local mode is preferred.

Note: you can install a module globally by adding “-g” parameter.

Important Note:Before installing a module,make sure you create a folder in a near destination.I will be using a test folder in my desktop.

Installing a Module

Installing a module is easy,lets get back to https://www.npmjs.com/ and select a module to install browserify:

node8

Now open your Nodejs Command Prompt and head to the folder you just created.

node9

Now add these lines to install browserify in the specified folder:

npm install browserify

Press Enter and let it download dependencies and browserify module itself.

node10
Once its finished,you can have a look at your folder structure being replaced by browserify module and its dependencies.

UnInstalling a Module

Since we installed Browserify,why dont we use the same module?

To Uninstall a module you simply,write this command:

npm uninstall browserify

node11

And it will uninstall browserify and its dependencies.

node12

You shall see in your project folder that the modules are gone.

Updating a Module

Why do we even update? To get the latest version of course!

If you previously used an older module with new version available,you’ll desire to update it in no time.

To Update a module you simply write this command:

npm update browserify

Rest is up to NPM.It will update the specified module.Since we uninstalled the browserify module previously however you can test it after you install it again:

node13

We talked about a great deal managing modules in this post.Hope it was useful information for you,Wizards 🙂

See you next time.

NodeJS Series #4: Creating a Port Listener

Standard

Hello Wizards,

First of all,sorry I couldn’t publish this post as I promised,i had illness.Even Wizards get sick!
The purpose of this post is all about creating a server,listening it through a port number and then sending output to the browser.

So lets start immediately!

We shall write a small script that creates server and listens to this object.

var http = require("http");

http.createServer(function (request, response) {
    response.writeHead(200, { "Content-Type": "text/plain" });
    response.write("Listening to localhost:8888");
    response.end();
}).listen(8888);

Lets take a breath and talk about what this script does in detail,shall we?

var http = require("http");

1st line of the code tells us it will use http object and implement it.You can realize this statement as importing a namespace to a project and referencing it.

**********************************

http.createServer(function (request, response) {

This code does create server object with 2 parameters: request and response.Clearly you know HTTP works according to Requests and Responses.Same thing here.

**********************************


    response.writeHead(200, { "Content-Type": "text/plain" });
    response.write("Listening to localhost:8888");
    response.end();

As mentioned earlier,when you request this server object,it will send 200 code(Success) and Content-Type Headers to the server object.Afterwards,we will write specifying as if it listens to the server via a port number and laters we end response object so that we cand send buffered output to the response and stop executing the page and thus ends the request.

**********************************


}).listen(8888);

Now thats the last thing, we should be doing : Listening to the port number.

You do realize we only used a port number here? That means we shall use “localhost” for testing purposes.

Cant we use an IP with port number? Yes you can!

If you have changed the code just like that:


}).listen(8888, '127.0.0.1'););

It would still work.

Now since we cleared a few things up here,we can test the server now!

Store the sample js file in a close location and then run Node.js Command Prompt:

node5

Press Enter.

You realize server object created and now listens to the localhost:8888 right? 🙂

So,open your favorite web browser and navigate to this server!

node7

As long as Nodejs Command Prompt runs,you can access the server object and do some stuff!

Remember one thing: Creating a server object is the 1st step creating a Rest service with Node.js.
And I plan on writing a post about it in the following series.

I hope this post was useful for you.

Cheers,Wizards!

NodeJS Series #3: Using External Files

Standard

Hello Wizards,

This time we will create external .js files to call from NodeJS Command Prompt.

Create a new .js file (for instance I’ve created sample.js).
Add these javascript lines as an example:

console.log("Poooff");

When we call external file from nodejs command prompt,it shall write “Pooff” as an output.

Store this file to a close destination,easy to access later from command prompt.Now open “Nodejs Command Prompt” application and call this:

node5

Then press enter and see what happens.According to me,I get this one:

node6

Its that easy to call external files with Nodejs.

Do you want much more complicated and real-life solution? Wait for tomorrow, Wizards 🙂

I shall show you how to listen to a port tomorrow.

NodeJS Series #2: Installing the Device and Casting a Simple Spell

Standard

Hello there Magicians,

Ready to enter to the world of Node Spells?

Lets install the Node.js now via http://nodejs.org

node1

Click on Install and start executable setup file.

node2

Complete the installation.

Then open “Node.js” application with green icon to start casting your spells.

node3

Lets say we want to write “Poooof” on the console,heres how:

console.log("Poooff");

node4

Well Done Wizard!
This was your first spell with Node Device and you’ve successfully did it!

Next time you shall see how to install and run seperate Node spells and master at it!

See you till next time.

NodeJS Series #1: An Introduction to Magical Device

Standard

nodejs-logo

Hi dear readers,

Its really amazing when you decide to start writing about most popular technologies and this is one of them.I’ve planned on publishing 1 article per day about this topic.It will be a real experience for you,too.

NodeJS is one of the most popular “things-to-learn” nowadays.
So my advice is add me to your bookmarks or simply “follow me” and read the series everyday as i’ll keep writing about it.

Lets cut to the chase and speak about what NodeJS is.

NodeJS? What the hell is that?

Once upon a time(in 2009), a young wizard named Ryan Dahl joined the JSConf summit where every wizards come together and show the audience their magic and tricks.

Ryan developed a device named “NodeJS” which is a complete product of “V8 JS Engine” from a good old magician club of Google and added more functionalities like Event Loop and low-level(hell) I/O API (which helps Ryan do more magic then usual)

This device’s main goal was enabling other wizards do more magic in a server-side and event-driven galactica.

After the success of the device,the wizards congratulated Ryan and contributed their magics to the device to make it better magic casting device.From that day forth,wizards called it “Node”

Node has been widely used by top wizards in the world and in return Node helped them to build magical networked services.

In a world of simple casts,the advantage of the Node was; it used Javascript in backend.
Since Javascript is the most used magic in this world,most web magicians used Javascript with Node to increase popularity of their magics.

The other advantage of Node was, other magicians also contributed some helper-magics to increase the usability and popularity of the Node device.From that day forth,Node device enabled wizards to do many more magics.This is a great time to be a magician,if you ask me 😉

And yes,it very easy to use device.If you own a Microsoft Wand,go to http://nodejs.org and download the device.

If you use Steve’s Apple Wand,you can also download the device and use it from http://nodejs.org 

I will let you know how to use the device everyday from now on.Dont you worry o’Magicians.I wont let you cast wrong spells with Node 🙂

See ya tomorrow.