nodejs
  1. nodejs-introduction

Node.js Introduction

Node.js is an open-source server-side JavaScript runtime environment built on Chrome's V8 JavaScript engine. It allows developers to build scalable and high-performance applications.

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. This makes it particularly suitable for real-time, data-intensive applications that run across distributed devices.

In this tutorial, we'll provide an introduction to Node.js, covering its syntax, example, output, explanation, use, important points, and summary.

Syntax

The basic syntax for creating a Node.js program is as follows:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080);

console.log('Server running at http://127.0.0.1:8080/');

In this example, we're creating a simple HTTP server that responds with "Hello World" for every request.

Example

Let's take a closer look at the example above and break it down.

var http = require('http');

This line of code imports the Node.js http module, which allows us to create HTTP servers and make HTTP requests.

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080);

This code creates an HTTP server that listens on the port 8080. Whenever a request is made to this server, it responds with the text "Hello World" and a status code of 200. The createServer function takes a callback function as an argument, which is called for every incoming request.

console.log('Server running at http://127.0.0.1:8080/');

This final line logs a message to the console, indicating that the server is running and can be accessed at http://127.0.0.1:8080/.

Output

When you run the above program, you'll see that the program is running, and a message is displayed in the console indicating that the server is running:

Server running at http://127.0.0.1:8080/

Now, if you open up a web browser and navigate to http://127.0.0.1:8080/, you should see the message "Hello World" displayed in your browser window.

Explanation

In the example above, we created a simple Node.js program that listens for incoming HTTP requests on port 8080. Whenever a request is made, the server responds with the text "Hello World".

We start by importing the Node.js http module. We then call the createServer function, which takes a callback function as an argument. This function is called for every incoming request and takes two arguments: req and res, which represent the HTTP request and response, respectively.

Inside the callback function, we set the status code of the response to 200, indicating that the request was successful. We also set the content type to "text/plain" and write the message "Hello World" to the response. Finally, we call the listen function, passing in the port number on which we want the server to listen.

Once the server is running, we log a message to the console indicating its URL.

Use

Node.js is used for building server-side applications and network applications. It's particularly well-suited for real-time, data-intensive applications that run across distributed devices.

Important Points

  • Node.js is built on Chrome's V JavaScript engine.
  • Node.js uses an event-driven, non-blocking I/O model.
  • Node.js is particularly suitable for real-time, data-intensive applications that run across distributed devices.
  • Node.js comes with a built-in package manager called npm.

Summary

In this tutorial, we provided an introduction to Node.js, covering its syntax, example, output, explanation, use, important points, and summary. With this knowledge, you can now create simple Node.js programs and understand how Node.js works.

Published on: