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:
- Open the Node.js command prompt.
- Navigate to the directory where you saved the file.
- 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:
- Import required modules – Use the
requiredirective to load thehttpmodule and assign it to a variable.var http = require("http"); - Create a server – Establish a server that listens for client requests, similar to an Apache HTTP server.
- 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
- Import Required Module
Load thehttpmodule using therequiredirective and store it in a variable. - Create Server
Usehttp.createServer()to create a server instance. Then, use thelisten()method to bind it to port8081. The function passed as an argument handles requests (request) and sends responses (response). - Combine Steps
Save the combined code in a file namedmain.js.
Running the Server
- Open the Node.js command prompt.
- Navigate to the location where you saved
main.js. For example, if it’s saved on the desktop:cd desktop - 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