Let’s Consider A Case:
Consider a website in which you need to load content dynamically from another web server which is slow. In PHP you can do it in 2 ways – coding it in a simple file and coding it as another script, then executing it in a multithreaded approach.
Getting Started With Node.Js
That said, the main highlight of Node.js – it is event-based asynchronous functions. It uses anevent loop instead of waiting for I/O operations (accessing external web service, accessing hardware).
Node.js could still make use of its processing power when the server is waiting for any other operation. This makes Node.js scalable to millions of concurrent connections.
Here are some provider suggestions for Node.js hosting:
Node will work perfectly on Linux, Macintosh, and Solaris operating systems. On Windows you can install it using the Cygwin emulation layer. None of the builds in Windows are satisfactory but it is still possible to get something running.
Option 1: Building Node from source.
Use make to build and install node.js (execute the following on the command line). Git is required.
It loads the sys class into a variable sys. It then uses the sys object to perform the console tasks. The sys.puts is a command similar to the cout in C++, so in order to run the script above go to the command prompt and run it by the command below:
If your installation is successful then you will get a hello world output in the screen.
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.
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:
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”.
Creating Simple Static File Server
Let’s create a simple static file server in the next tutorial.
The request object has the request details as we have discussed earlier. We use the parsefunction of the URL class which we have included to get the pathname of the request URL. After getting the pathname we concatenate it with the path of the current working directory in order to get the full path of the file.
For joining URLs we have a function called join in the path library.
Now in the callback function if the file does not exist we send a "404 Page Not Found" error.
If the page is found then we read the file by the readFile function in file system. We can also see the callback function for the readFile function defined there itself. If there is no error in reading the file then it will be displayed. If there is an error then a status 500 is returned with the error text.
I also recommend wrapping codes of the previous tutorial into a function so that you can use it in the next tutorial or for future use.