Get in touch
or send us a question?

[Node.js Tutorials for Beginners] – Unit 2: First Example


Note: In this tutorial, I will using Visual Studio Code to development and run Node.js code. Link to install VS Code on Ubuntu:

There can be console-based and web-based node.js applications

1. Node.js console-based Example

We create a new folder (depending on where you are), I create a folder name: nodejs. Next, I create a new file named app.js in here and write the following code:

console.log(‘Hello, World’)

Here, console.log() function displays message on Console

2. Node.js web-based Example

A node.js web application contains the following three parts:

+ Import required modules: The “require” directive is used to load a Node.js module.

+ Create server: You have to establish a server which will listen to client’s request similar to Apache HTTP Server

+ Read request and return response: Server created in the second step will read HTTP request made by client which can be a browser or console and return the response.

How to create node.js web applications

Create a file named: main.js and follow these steps:

1. Import required module: For example:

var http = require(“http”);

2. Create server:

http.createServer(function(request, response){

response.writeHead(200, {‘Content-Type’: ‘text/plain’});

response.end(‘Hello, World\n’);


console.log(‘Server running at‘);

3. Read request and return response:

I save main.js and start the server as bellows:

$ node main.js

Now server is started:

Make a request to Node.js server:

Open in any browser. You will see the following result

Now, if you make any changes in the “main.js” file, you need to again run the “node main.js” command.

Through the above article, you have learned first example with Node.js, this is the background step for you to continue in the next articles 😀

Thank you!