Learnwizy Technologies Logo

Learnwizy Technologies

Class 20: Introduction to Backend Development & Node.js Basics

Welcome to the backend! Up until now, we've focused on the "client-side" of web development—what users see and interact with directly in their browsers. Now, we'll shift our attention to the "server-side," the powerful engine that processes data, manages databases, and serves content to the frontend.


The Role of the Backend

In the world of web development, the client-server model is fundamental.

The backend is responsible for several critical tasks:


Introduction to Node.js

Traditionally, backend development involved languages like Java, Python, Ruby, or PHP. Node.js changed the game by allowing developers to use JavaScript on the server-side.


Node Package Manager (npm)

npm (Node Package Manager) is the default package manager for Node.js and the world's largest software registry. It's used for installing, managing, and sharing reusable code packages (libraries) for your Node.js projects.

Basic npm Commands:


Basic Node.js Modules

Node.js has a rich set of built-in modules that provide core functionalities without needing to install external packages. You import them using the require() function (CommonJS syntax, common in Node.js backend) or import (ES Modules, gaining popularity).


Building a Simple "Hello World" HTTP Server

Let's put some of these concepts into practice by creating a very basic web server using Node.js's built-in http module.

Create a file named server.js (or index.js) in your project directory:

// server.js
const http = require('http'); // Import the built-in http module

const hostname = '127.0.0.1'; // Localhost IP address
const port = 3000; // Port number for our server to listen on

// Create an HTTP server
const server = http.createServer((req, res) => {
  // req is the request object from the client
  // res is the response object we send back to the client

  // Set the response HTTP header with a status code and content type
  res.statusCode = 200; // 200 OK
  res.setHeader('Content-Type', 'text/plain'); // Tell the client it's plain text

  // Basic routing logic
  if (req.url === '/') {
    res.end('Hello, World! This is the homepage.');
  } else if (req.url === '/about') {
    res.end('This is the about page. Learn more about us!');
  } else {
    res.statusCode = 404; // Not Found
    res.end('Page Not Found');
  }
});

// Start the server and listen for incoming requests
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
  console.log('You can access:');
  console.log(`- http://localhost:${port}/`);
  console.log(`- http://localhost:${port}/about`);
  console.log(`- http://localhost:${port}/some-other-path`);
});

How to Run This Server:

  1. Make sure you have Node.js installed.
  2. Open your terminal or command prompt.
  3. Navigate to the directory where you saved server.js.
  4. Run the command:
    node server.js
  5. You should see the message:
    Server running at http://127.0.0.1:3000/
  6. Open your web browser and navigate to:
  7. To stop the server, press Ctrl + C in your terminal.

This simple example demonstrates how Node.js can act as a web server, listen for requests, and send responses. While the http module is powerful, directly managing routing and complex request handling can become cumbersome. This is where frameworks like Express.js come in, which we'll explore in the next class!