Node.js

What is the Node.js

Pramud Nadula
4 min readMay 14, 2022
node.js

Ryan Dahl created Node.js in 2009, roughly thirteen years after Netscape’s LiveWire Pro Web introduced the first server-side JavaScript environment. Only Linux and Mac OS X were supported in the initial release. Dahl was in charge of its development and maintenance, and Joyent later sponsored it.

Dahl chastised the most popular web server in 2009, Apache HTTP Server, for its limited ability to handle a large number of concurrent connections (up to 10,000 or more) and the most common programming style (sequential programming), in which code either blocked the entire process or implied multiple execution stacks in the case of simultaneous connections.

Node Js

Node.js is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Node.js allows developers to use JavaScript to create command-line tools and server-side scripting, which involves running scripts on the server before sending the page to the user’s browser. As a result, Node.js represents a “JavaScript everywhere” paradigm, bringing web application development together around a single programming language rather than separate languages for server-side and client-side scripts.

Node.js shines in real-time web applications that use WebSockets and push technology. We now have web applications with real-time, two-way connections. The client and server can initiate communication, allowing them to freely exchange data, after more than 20 years of stateless-web based on the stateless request-response paradigm. This is in sharp contrast to the typical web response paradigm, in which the client is always the one to initiate communication. Additionally, it’s all based on the open web stack (HTML, CSS, and JS) running over the standard port 80.

Node JS Architecture

Node JS Architecture

To handle multiple concurrent clients, Node.js employs the “Single Threaded Event Loop” architecture. The JavaScript event-based model and the JavaScript callback mechanism are the foundations of the Node.js Processing Model.

Parts of the Node.js Architecture:

  1. Requests: Depending on the tasks that a user wants to perform in a web application, incoming requests can be blocking (complex) or non-blocking (simple).
  2. Node.js Server: The Node.js server is a server-side platform that accepts user requests, processes them, and returns responses to those users.
  3. Events Queue: A Node’s Event Queue. The js server saves incoming client requests and sends them to the Event Loop one by one.
  4. Thread Pool: The thread pool contains all of the threads that are available to perform some tasks that may be required to fulfill client requests.
  5. Event Loop: Event Loop receives and processes requests indefinitely, then returns responses to corresponding clients.
  6. External Resources: Blocking client requests necessitates the use of external resources. These resources could be used for computation, data storage, or other purposes.

Advantages of Node Js

  1. Using Event Queue and Thread Pool, the Node.js server can efficiently handle a large number of requests.
  2. Because Event Loop processes all requests one at a time, there is no need to create multiple threads; a single thread is sufficient.
  3. Because requests are handled one at a time, the entire process of serving requests to a Node.js server consumes less memory and server resources.
node js advantages

how to create node js server

Two ways to create node js server,

  1. http
/* ====== create node.js server with core 'http' module ====== */
//dependencies
const http = require("http");
// PORT
const PORT = 3000;
// server create
const server = http.createServer((req, res) =>
{
if (req.url === "/")
{
res.write("This is home page.");
res.end();
}
else if (req.url === "/about" && req.method === "GET")
{
res.write("This is about page.");
res.end();
}
else
{
res.write("Not Found!");
res.end();
}
});
// server listen port
server.listen(PORT);
console.log(`Server is running on PORT: ${PORT}`);

2. express

/* ============================ **** ============================ */
/* ====== create node.js server with express.js framework ====== */
/* ==== First we need to install the express using npm command ==== */npm install express// dependencies
const express = require("express");
const app = express();
app.get("/", (req, res) =>
{
res.send("Hello World");
});
app.post("/", (req, res) =>
{
res.send("This is home page with post request.");
});
/* ==== we need to specify the port number that our server need to run ==== */// PORT
const PORT = 3000;
/* ==== app.listen() used to bind and listen the connections on the specified host and port ==== */app.listen(PORT, () =>
{
console.log(`Server is running on PORT: ${PORT}`);
});

Instructions

// save this as index.js
// you have to download and install node.js on your machine
// open terminal or command prompt
// type node index.js
// Then load the server at http://localhost:3000

I hope you found this useful. I would like to hear from you so feel free to drop a comment or connect with me via LinkedIn or Portfolio.

--

--