NodeJS

NodeJS Series #4: Creating a Port Listener

Sharing is Caring

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.

[sourcecode language=”javascript”]
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);

[/sourcecode]

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

[sourcecode language=”javascript”]
var http = require("http");
[/sourcecode]

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.

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

[sourcecode language=”javascript”]
http.createServer(function (request, response) {
[/sourcecode]

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.

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

[sourcecode language=”javascript”]

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

[/sourcecode]

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.

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

[sourcecode language=”javascript”]

}).listen(8888);

[/sourcecode]

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:

[sourcecode language=”javascript”]

}).listen(8888, ‘127.0.0.1’););

[/sourcecode]

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!

Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir