Open In App
Related Articles

Node First Application

Like Article
Save Article
Report issue

Node is an open source, cross-platform server environment which executes JavaScript using V8 JavaScript Engine. Node helps to write front-end and back-end code in the same language. It helps to write efficient code for real-time applications.

We will discuss the following two approaches to create our first application in Node:

Creating Console based Node Application:

The Node console-based applications are run using Node command prompt. Console module in Node.js provide a simple debugging console. Node is a global console which can be used for synchronous as well as asynchronous communication. The console.log() function is used to display output on console. This function prints output to stdout with newline.


console.log([data][, ...]);

Here, data is the content to be displayed on console.

Example 1: Creating a Hello World application using Node.js. Create a geeks.js file containing the following code:


console.log('Hello World');


Run the file on Node.js command prompt using command node geeks.js i.e. node <file_name> .


Example 2: Creating a Hello World application receiving the user input. Create a gfg.js file containing the following code.




The process.argv is used to provide command line argument to a program. Use the slice function with 2 as its argument to get all the elements of argv that comes after its second element, i.e. the arguments the user entered The first argument is location of the Node.js binary which runs the program and the second argument is location of the file being run.


Creating Web-based Node Application:

A web-based Node application consists of the following three important components:

  • Import required module
  • Create server
  • Read Request and return response

Let us learn more about them in detail

Import required modules

Load Node modules using the require directive. Load http module and store returned HTTP instance into a variable.


var http = require("http");

Creating a server in Node:

Create a server to listen the client’s requests. Create server instance using createServer() method. Bind server to port 8080 using listen method associated with server instance.



Read request and return response in Node:

Read the client request made using browser or console and return the response. A function with request and response parameters is used to read client request and return response.


http.createServer(function (request, response) {...}).listen(8080);

Example:This example create a Hello World web-based application using Node.js. Create a firstprogram.js file containing the following code.


// Require http header
var http = require('http');
// Create server
http.createServer(function (req, res) {
    // HTTP Status: 200 : OK
    // Content Type: text/html
    res.writeHead(200, {'Content-Type': 'text/html'});
    // Send the response body as "Hello World!" 
    res.end('Hello World!');


Step to run the application:

Step 1: To run the program type the following command in terminal

node firstprogram.js  

Step 2: Then type the following URL in browser


Last Updated : 27 Feb, 2024
Like Article
Save Article
Share your thoughts in the comments
Similar Reads