Node.js: Create a Basic Application [Beginner Tutorial]

Node.js is an open-source runtime environment, used to build server-side and networking application using Javascript. Generally, Javascript has been used for client-side scripting. However, with Node.js, you can interact with web-server using Javascript. The APIs of Node.js are asynchronous and event-driven which helps in faster code execution.

Here’s the installation instruction for Node.js.

Creating Basic Hello World application using Node.js

This can also be said as Creating your very First Application in Node.js.

Below is the code for a simple hello world application in node.js.

At first, we will be importing a built-in Node.js module named http. A Node.js module is like a Javascript library that provides certain features. require function is used to include/import the module.

After that, we create server using the http module. The server will listen to a particular port. You can provide any port that is available on your computer. That can be like 8080, 8081, 8888, 3000, etc. The server will read the HTTP request and return a response. We will be listening to port 8081.

response.writeHead() function will write the header of the response.

response.write() function will send the content to the response.

response.end() function can also write the body and this will also close the response.

server.js


var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('Hello World');
    response.end('Ending message</p>');
}).listen(8081);

// This prints the message in the terminal when you run "node server.js"
console.log('Server running at port 8081: http://127.0.0.1:8081 or http://localhost:8081');

Now,

  • open terminal or command-prompt
  • go to the directory where your server.js file is
  • after you are in the directory where the above created server.js file is, then run the following command:
node server.js

This will print the message in your terminal/command-prompt saying “Server running at port 8081…” like below:


mukesh@chapagain:/var/www/html/test/nodejs$ node server.js
Server running at port 8081: http://127.0.0.1:8081 or http://localhost:8081

Now, open your browser and type the following in your browser’s address bar:

http://127.0.0.1:8081 orhttp://localhost:8081

Press Enter and you shall be able to see “Hello World” written.

That’s it! You have successfully created a Node.js server. You have created an HTTP request and sent out the response as:

Hello World<p>Ending message</p>

Working with HTML code

The above example’s content-type was text/plain. Therefore, HTML tags do not work in the output response. To make the HTML code work, you need to change the content-type of the code to text/html.

server.js


var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.write('Hello World');
    response.end('Ending message</p>');
}).listen(8081);

// This prints the message in the terminal when you run "node server.js"
console.log('Server running at port 8081: http://127.0.0.1:8081 or http://localhost:8081');

Run the app:

node server.js

Now, when you browse http://127.0.0.1:8081 or http://localhost:8081, you will see the following output:

Hello World

Ending message

The paragraph tag is working fine now.

Hope this helps. Thanks.