Posted in NodeJS

NodeJS Series #4: Creating a Port Listener

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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s