Node.js Basic Introduction & Tutorial

Node.JS is a serverside javascript(JS) implementation. Why should JS? because JS has reliability on the eventing and callback side. For your information, until now server side applications are very difficult to make event-based, in fact most of them are thread-based.

Then Node.JS was born, the point is, with only one source coding, you can run lots of commands in parallel, that can happen because it’s based on events and callbacks.

Nod.js
Image by by Pedro Lozano

So computer resources such as processor and I/O can work independently. For example, we need to read data from the hard disk, the reading process is usually relatively long compared to the process in the processor. If in normal programming (eg PHP), we wait until the reading process is complete, then the next step. Now, while waiting for this, the processor is actually a waste because it doesn’t do anything.

In NodeJS, the read command was sent to I/O, then NodeJS doesn’t wait, but does the next command. When the I/O read process was finished, I/O sent an event to NodeJS. Just now the NodeJS program consumes the results of the previous reading.

So it can be seen that the processor is not completely redundant, 100% is used to run useful processes. then if we see, most servers are actually just routing from one I/O to another, for example from file to network, from database to network or from network to other network.

In ordinary programming, it is very difficult to create applications that run several commands in parallel. It’s the opposite with Node.JS
 making parallel simultaneous jobs is very easy, but the opposite makes commands for serial jobs a bit difficult.

Well, the explanation is complete, let’s go straight to the tutorial 🙂

Step 1: Installation
Download the latest version of Node.js at http://nodejs.org/
The version of Node.js I use is node-v0.10.25-x64 with windows 8.1 operating system 🙂
Install the node.js file that was downloaded earlier
Step 1 installation of Node.js
Step 1 installation of Node.js
Step 2 Node.js installation
Step 2 Node.js installation
Step 3 Node.js installation
Step 3 Node.js installation

Step 4 Node.js installation
Step 4 Node.js installation

Step 2: Hello World!
Well, every new technology usually starts with the words “Hello World!” here. So let’s try to create Hello World with Node.js 🙂

So we’re going to create a simple HTTP server that displays that message. First, you must understand the Node.js module system. In Node.js, functions are defined in modules that must be loaded in order to be used. There are many modules listed in the Node.js documentation which can be seen here

Now let’s create a file named index.js , then fill in the following code:

[js]
var http = require(‘http’);

http.createServer(function (req, res) {
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello Worldn’);
}).listen(8090, ‘127.0.0.1’);
console.log(‘Server running at http://127.0.0.1:8090/’);

[/js]

Then open a command prompt, navigate to the location where the index.js file is stored
Then type "node index.js" (without the quotes)
So the result is something like this picture:

Node.js Output output
Node.js Output output

then open the browser, because we set the port to 8090, then open http://localhost:8090/

and the display will look something like this:

Hello World Node.js
Hello World Node.js

That's my tutorial about Node.js .

Wait for the next article :)

Share To Social Media