Creating A HTTP Server

Now it’s time to create a "Hello World" via web server using Node.js.
Here’s what we are going to do – we create a server that outputs a
“Hello World” to the localhost on the port 8080 no matter what the URL
is, giving you an idea what event is.

Code explanation:

The most interesting part in Node.js is its event-based programming. In
order to create a HTTP server we need the HTTP library, so we go forward
and add it using my_http. We create server by the function:

view plaincopy to clipboardprint?
my_http.createServer(function(request,response){}).listen(8080);
The function given as the first argument is executed every time an event
is triggered in port 8080, so the function itself suggests the node to
listen for an event in port 8080. In order to detect this, I have added
a “I got kicked” message which will be displayed on the console screen
whenever a request is received.

The request object contains all the information about the request that
has been made to the server. For example it contains the URL string. The
response object is the object that handles the response from the server.
First we set the header of the response as a text/plain content, then
outputs “Hello World“, then end the output stream. 200 is the status
response.

Well, the above one is a very simple example but we can see that
whatever URL we give in the browser for the same server we get the same
output like “Hello World”.