Node.js Tutorial Part-2 – Hello World in a web browser

Let’s keep it simple, but realistic:

  • The user should be able to use our application with a web browser

Application Stack

  • We want to serve web pages, therefore we need an HTTP server
  • Our server will need to answer differently to requests, depending on which URL the request was asking for, thus we need some kind of router in order to map requests to request handlers
  • To fullfill the requests that arrived at the server and have been routed using the router, we need actual request handlers
  • The router probably should also treat any incoming POST data and give it to the request handlers in a convenient form, thus we need request data handling
  • We not only want to handle requests for URLs, we also want to display content when these URLs are requested, which means we need some kind of view logic the request handlers can use in order to send content to the user’s browser
  • Last but not least, the user will be able to upload images, so we are going to need some kind of upload handling which takes care of the details

Let’s think a moment about how we would build this stack with PHP. It’s not exactly a secret that the typical setup would be an Apache HTTP server with mod_php5 installed. 

Well, with node, things are a bit different. Because with Node.js, we not only implement our application, we also implement the whole HTTP server. In fact, our web application and its web server are basically the same.

let’s create a main file which we use to start our application, and a module file where our HTTP server code lives.

My impression is that it’s more or less a standard to name your main fileindex.js. It makes sense to put our server module into a file named server.js.

Let’s start with the server module. Create the file server.js in the root directory of your project, and fill it with the following code:

var http = require("http");

http.createServer(function(request, response){
  response.writeHead(200,{"Content-Type":"text/plain"});
  response.write("Hello World");
  response.end();}).listen(8080);

That’s it! You just wrote a working HTTP server. Let’s prove it by running and testing it. First, execute your script with Node.js:


node server.js

Now, open your browser and point it at http://localhost:8080/ or curl http://localhost:8080 also.This should display a web page that says “Hello World” like shown below.

23-05-2014 12-44-31


Analyzing our HTTP server


Well, then, let’s analyze what’s actually going on here.

The first line requires the http module that ships with Node.js and makes it accessible through the variable http.

We then call one of the functions the http module offers: createServer. This function returns an object, and this object has a method named listen, and takes a numeric value which indicates the port number our HTTP server is going to listen on.

Please ignore for a second the function definition that follows the opening bracket of http.createServer.

We could have written the code that starts our server and makes it listen at port 8080 like this:

 
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