Nodejs First Example

Nodejs First Example

Node.js applications can be of two main types: console-based and web-based. Let’s walk through simple examples of both to help you get started.

Introduction to Applied AI:-Click Here

Node.js Console-Based Example

File: console_example1.js

console.log('Hello UpdateGadh');

How to Run It:

  1. Open the Node.js command prompt.
  2. Navigate to the directory where you saved the file.
  3. Run the following command:

node console_example1.js

Explanation:
The console.log() function simply prints the message to the console. This is the most basic way to test that Node.js is installed and working correctly.

Node.js Web-Based Example

A simple Node.js web application generally includes three steps:

  1. Import required modules – Use the require directive to load the http module and assign it to a variable. var http = require("http");
  2. Create a server – Establish a server that listens for client requests, similar to an Apache HTTP server.
  3. Read requests and return responses – The server will handle incoming HTTP requests and send back a response.

Data Science Tutorial:-Click Here

Complete Example

File: main.js

var http = require("http");

http.createServer(function (request, response) {  
   // Send the HTTP header   
   // HTTP Status: 200 : OK  
   // Content Type: text/plain  
   response.writeHead(200, {'Content-Type': 'text/plain'});  

   // Send the response body  
   response.end('Hello World\n');  
}).listen(8081);  

// Console will print this message  
console.log('Server running at http://127.0.0.1:8081/');

Explanation of Steps

  1. Import Required Module
    Load the http module using the require directive and store it in a variable.
  2. Create Server
    Use http.createServer() to create a server instance. Then, use the listen() method to bind it to port 8081. The function passed as an argument handles requests (request) and sends responses (response).
  3. Combine Steps
    Save the combined code in a file named main.js.

Running the Server

  1. Open the Node.js command prompt.
  2. Navigate to the location where you saved main.js. For example, if it’s saved on the desktop: cd desktop
  3. Start the server with: node main.js

Once the server starts, the console will display:

Download New Real Time Projects :-Click here

Server running at http://127.0.0.1:8081/

Test Your Server

Open a browser and visit:

http://127.0.0.1:8081/

You should see the output:

Hello World

If you make changes to main.js, restart the server by running node main.js again.

Machine Learning Tutorial:-Click Here

Complete Advance AI topics:- CLICK HERE
Deep Learning Tutorial:– Click Here

Conclusion
This was your very first step into building applications with Node.js — starting from a simple console example to a basic web server. As you move forward, you can expand these concepts into more complex and powerful applications.


how to run node js in terminal
sample node-js application github
node js download
node js run command
node js hello world example
how to run node js in visual studio code terminal
express js
node js tutorial
node js first example w3schools
node js first example github
node js first example for beginners
node js first example in javascript
node js first example geeksforgeeks

Post Comment