Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

What is Node JS Explaination ?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. It is built on Chrome’s V8 JavaScript engine and allows developers to run JavaScript on the server-side, enabling the development of scalable and high-performance network applications. Node.js uses an event-driven, non-blocking I/O model, which makes it lightweight and efficient, suitable for building real-time applications like web servers, chat applications, APIs, and more. Additionally, Node.js has a vast ecosystem of libraries and frameworks available via npm (Node Package Manager), making it a popular choice for developers for building various types of applications.

Advantage of Nodejs

  1. Open source
  2. Efficient Fast and highly scalable
  3. Event Driven
  4. Very popular

Example:-

Let’s create a simple HTTP server using Node.js. This server will listen for incoming HTTP requests and respond with a basic “Hello, World!” message.

// Import the 'http' module
const http = require('http');

// Define the hostname and port number
const hostname = '127.0.0.1';
const port = 3000;

// Create a server
const server = http.createServer((req, res) => {
  // Set the response header
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  
  // Send the response body (Hello, World!)
  res.end('Hello, World!\n');
});

// Start the server and make it listen on the specified hostname and port
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

In this example:

  1. We import the built-in http module, which allows us to create an HTTP server.
  2. We define the hostname and port number on which our server will listen for incoming requests.
  3. We create an HTTP server using the http.createServer() method. This method takes a callback function that will be called whenever a request is received by the server.
  4. Inside the callback function, we set the response header using res.writeHead() and send the response body with res.end().
  5. Finally, we start the server by calling server.listen() and passing it the port, hostname, and a callback function to be executed once the server starts.

When you run this Node.js script, it will start an HTTP server on http://localhost:3000/. If you visit this URL in a web browser or make a request using tools like cURL or Postman, you’ll receive the response “Hello, World!” from the server.

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

Youtube Subscriber Count in ReactJs

In this tutorial i’m going to learn how to count YouTube Subsribers count, views count as well, as define below. In order to install your app, first…

Explaination of Module in Node Js with Example ?

What is Module in Node Js? In Node.js, a module is essentially a reusable piece of code that encapsulates related functionality. Modules help in organizing code into…

Hellow word in Node js with example ?

What is Node js ? Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is…

Building HTTP Server in NodeJS with example ?

What is Node js ? Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is…

What is Node JS with example ?

Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is built on the V8 JavaScript…

How to Building HTTP Server in NodeJS

What is Node js ? 1st step create below file and put below code Next go to package.json and put below code Next to run below command…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x