Node.js First Application - GeeksforGeeks

13 Oct, 2021
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:
console.log('Hello World'); 
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.
console.log(process.argv.slice(2));
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");
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);
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);
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 headervar http = require('http');   // Create serverhttp.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);
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:
Node.js-Basics
Picked
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
How to update NPM ?
Difference between promise and async await in Node.js
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript