Introduction to Node.js

What is Node.js?

Node.js is a runtime environment that allows developers to execute JavaScript code on the server side. Unlike traditional JavaScript, which runs in a browser, Node.js enables you to write backend server logic and interact with files, databases, and networks directly.

Why use Node.js
  • Cross-platform :  Available for Windows, macOS, Linux, etc.
  • JavaScript on the server : JavaScript on the server enables using the same language for both frontend and backend.
  • Event-driven and non-blocking I/O : Optimized for scalable network applications.
Introduction to Server-Side JavaScript

Server-side JavaScript, powered by Node.js, means running JavaScript outside the browser to handle backend logic like managing databases, serving content, and interacting with the operating system. This enables full-stack JavaScript development where a single language can manage both frontend and backend.

				
					// Importing HTTP module to create a server
const http = require('http');

// Creating a simple server
http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello from the server-side JavaScript!');
}).listen(3000);

console.log('Server running at http://localhost:3000/');
				
			
History and Purpose of Node.js
  • Creator : Ryan Dahl introduced Node.js in 2009.
  • Purpose : The goal was to create an environment where JavaScript could be executed server-side, overcoming the limitations of traditional request/response models used by Apache or similar servers.
  • Non-blocking I/O : Node.js uses a single-threaded event loop to handle multiple connections efficiently, making it suitable for real-time applications (like chat apps, gaming, etc.).
Major Achievements
  • 2009: Initial release.
  • 2011: npm (Node Package Manager) was introduced.
  • 2015: Node.js Foundation was created.
How Node.js Works
  • Event-Driven Architecture: Node.js uses events to manage asynchronous tasks. Instead of waiting for tasks (like file reading or database queries), Node.js moves on to the next task and returns to handle the results later.

  • Non-blocking I/O: Node.js uses a single thread to handle multiple connections asynchronously. Traditional I/O operations (like reading files) do not block the execution of other code.

				
					const fs = require('fs');

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

console.log('This will execute before file content is displayed!');
				
			
Installing Node.js and npm

To use Node.js, you need to install it on your system.

  1. Installation :

    • Visit the official Node.js website and download the appropriate installer for your operating system (Windows, macOS, Linux).
    • The installer will also include npm (Node Package Manager).
  2. Verify Installation : After installation, open the terminal and run.
  3. This should display the versions of Node.js and npm installed.
				
					node -v
npm -v
				
			
Creating the First Node.js Application: Hello World

Once Node.js is installed, you can create your first application. Let’s build a simple “Hello World” HTTP server.

  1. Steps to Create a Basic Node.js App 

    • Create a new file called app.js
				
					// Import the HTTP module
const http = require('http');

// Create a server object
http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World from Node.js!');
  res.end(); // End the response
}).listen(8080); // The server listens on port 8080

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

      2. Run the App

  • Open your terminal, navigate to the directory containing app.js, and run.
  • Open your browser and go to http://localhost:8080/. You should see the message “Hello World from Node.js!”.
				
					node app.js
				
			

Important Points :

  • Node.js allows JavaScript to be used for both frontend and backend development.
  • Its non-blocking, event-driven architecture makes it highly efficient for I/O-heavy tasks.
  • Node.js is widely used as the backbone of modern web applications.
  • Learning basic Node.js is a solid foundation for creating complex server-side logic.