Node.js | First Application

Node.js is an open source, cross-platform server environment which executes JavaScript using V8 JavaScript Engine. Node.js helps to write front-end and back-end code in the same language. It helps to write efficient code for real-time applications. In Node.js, the applications can be written using console-based method or web-based method.

Console based Node.js Application: The Node.js console-based applications are run using Node.js command prompt. Console module in Node.js provide a simple debugging console. Node.js 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.

Syntax:

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

console.log('Hello World'); 

chevron_right


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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

console.log(process.argv.slice(2));

chevron_right


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.
Output:

Web-based Node.js Application: A web-based Node.js application consists of the following three important components:

  • Import required modules: Load Node.js modules using the require directive. Load http module and store returned HTTP instance into a variable.
    Syntax:

    var http = require("http");
  • Create server: 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.
    Syntax:

    http.createServer().listen(8080);
  • Read request and return response: 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.
    Syntax:

    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.

filter_none

edit
close

play_arrow

link
brightness_4
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!');
  
}).listen(8080);

chevron_right


Run the file on Node.js command prompt using command node firstprogram.js and type http://127.0.0.1:8080/ in a web browser to see the output.
Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.