Node.js Basics

Pranav Bakare - Sep 17 - - Dev Community

Node.js is a JavaScript runtime environment that allows developers to execute JavaScript code outside of a web browser. Built on Chrome's V8 JavaScript engine, it uses an event-driven, non-blocking I/O model, which makes it lightweight, efficient, and ideal for data-intensive applications that require handling multiple concurrent connections, such as web servers, APIs, and real-time applications.

Key aspects of Node.js include:

Asynchronous: Handles multiple requests simultaneously without waiting for operations to complete.

Single-threaded: Runs on a single thread but uses an event loop to manage many tasks efficiently.

Cross-platform: Can be deployed on Windows, macOS, and Linux.

NPM (Node Package Manager): A vast ecosystem of libraries and tools available to streamline development.

Node.js enables developers to build scalable, high-performance applications using JavaScript on the server side.

Here are the top 5 features of Node.js with sample examples for each:

1. Asynchronous and Non-Blocking I/O

Node.js allows asynchronous execution of functions, meaning the server can handle multiple requests simultaneously without waiting for an operation to finish.

Example:


const fs = require('fs');

// Asynchronous file reading
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error("Error reading file:", err);
    return;
  }
  console.log("File content:", data);
});

console.log("This prints before the file content is read!");

Enter fullscreen mode Exit fullscreen mode

Explanation: The file is read in a non-blocking manner, allowing other code to execute while waiting for the file operation to complete.

2. Single-Threaded with Event Loop

Despite being single-threaded, Node.js uses an event loop to handle multiple concurrent requests efficiently.

Example:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World!\n');
});

server.listen(3000, () => {
  console.log('Server running at http://127.0.0.1:3000/');
});

Enter fullscreen mode Exit fullscreen mode

Explanation: The event loop allows the server to handle multiple incoming requests without creating a new thread for each request.

3. Fast Execution with V8 Engine

Node.js is built on Google’s V8 JavaScript engine, known for its speed and performance.

Example:

console.time('Execution Time');

let sum = 0;
for (let i = 0; i < 1e6; i++) {
  sum += i;
}

console.timeEnd('Execution Time');

Enter fullscreen mode Exit fullscreen mode

Explanation: The code calculates the sum of numbers and uses console.time to demonstrate how quickly it executes, thanks to the V8 engine’s optimizations.

4. NPM (Node Package Manager)

Node.js has a built-in package manager, NPM, which gives access to thousands of open-source libraries and tools.

Example:

npm install express

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(3000, () => {
  console.log('Express server listening on port 3000');
});

Enter fullscreen mode Exit fullscreen mode

Explanation: In this example, we install and use the Express framework, a popular web framework available through NPM, to create a simple web server.

5. Cross-Platform Compatibility

Node.js applications can be deployed across different platforms like Windows, macOS, and Linux.

Example:

const os = require('os');

console.log('Platform:', os.platform());
console.log('Architecture:', os.arch());

Enter fullscreen mode Exit fullscreen mode

Explanation: This script uses the os module to check and print the current platform and system architecture, showing Node.js's ability to work across different environments.

These features make Node.js highly versatile for building high-performance, scalable applications.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .