In our last tutorial on Node.js in which we dealt with installing Node.js on Windows, we gave step-by-step instructions on the installation process of Node.js. But, we did not show how to write programs in Node.js. Moving a step further, in this tutorial we are going to show the basics of writing and running programs in Node.js.
An overview of what we will do in this tutorial:
- We will create a JavaScript file named server.js containing the code for creating a simple HTTP server using the core HTTP module of Node.js. This server will serve a Hello World message in HTML format when accessed from a browser.
- Keep this server.js file in the directory "C:\mynode".
- Run the code contained in server.js using Node.js.
- Access this server from a browser.
Let's start programming in Node.js.
1) Create the server.js file.
You will need a text editor, You can use your favorite JavaScript editor. Create a new JavaScript file, write the following code inside it, and save the file as server.js. You will find the explanation of this code in the later part of this tutorial.
server.js
var httpObj = require('http');
var htmlContent = "<!DOCTYPE html><html><head><title>Node.js Hello World tutorial from Qoncious.com</title></head><body><h1>Hello World from Node.js. It works!</h1></body></html>";
httpObj.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(htmlContent);
res.end();
}).listen(8012, '127.0.0.1');
console.log('Web server is running at http://localhost:8012');
Create a new folder in C: drive. Name the folder as mynode, and keep the server.js file in this folder.
2) Run the code in server.js using Node.js.
Open Command Prompt, change the current working directory to C:\mynode.
cd "C:\mynode"
Run server.js using the command shown below.
node server.js
3) Access the server from a Browser.
Visit localhost:8012 or 127.0.0.1:8012 from a web browser. If everything works fine, you will see a Hello World message in your Browser.
What we are doing in the program?
1) Include the core HTTP module.
The first thing that we are doing in the code is to include the core HTTP module (some developers call it as the core HTTP library) into the program by using require('http'). The function "require" returns an object containing the API of the HTTP module.
2) Assign some HTML to a variable.
We then assigned some HTML content to the variable htmlContent. This HTML will be served to the browser.
3) Create an HTTP server in Node.js.
After that, we created a server using the createServer() method of the HTTP module through the httpObj object. Observe the createServer() method carefully. It accepts a function as a parameter. We passed an anonymous function as the argument to the createServer method.
Now the important part. We know that Node.js is event-driven. The function that we passed to the createServer method is assigned as the Event Listener for the 'request' event. In simple language it means that whenever there is an HTTP request (i.e. HTTP request sent from the browser to the server), this function(callback) will be executed.
Next, we told the server where to listen for the requests using the server.listen(8012, 127.0.0.1) function call. (The first argument is port and the second argument is the hostname).
4) The anonymous function.
If you look at this anonymous function that we passed as argument to the createServer method, you will see that it accepts two parameters, one parameter is for the HTTP request object and the other is for the HTTP response object. In this function we are sending the HTTP response to the client Browser.
The response.writeHead method.
The line res.writeHead(200, {'Content-Type': 'text/html'}) sends the response header to the request. The first argument of the writeHead method is the HTTP status code. (You must be familiar with some status codes like 404 Not Found. Status code 200 OK is for successful HTTP requests.) The second and the last argument tells that the response is an HTML document.
The response.write method.
The line res.write(htmlContent) is for sending response content to the client browser.
The response.end method.
Then we called the response.end() method. Without the call to the response.end() method, no response will be sent to the client browser. So, calling this method is necessary to send the response.
So, this was a Hello World tutorial on Node.js for beginners containing 10 lines of code. Although this tutorial touched only the very basics of Node.js programming, it works!
You can give your suggestions using the suggestion box below.