DAY 1 to BackEnd Dev

Starting with Backend Development: A Beginner's Guide Using Express.js

If you're just starting your journey into web development, understanding the backend can feel a little mysterious at first. You hear terms like server, routes, requests, dependencies, and it can all get confusing really fast.

In this beginner-friendly guide, we’ll break down the core backend concepts using Express.js, one of the most popular web frameworks in the JavaScript ecosystem. We'll explain what everything means step by step, and by the end, you’ll understand how to build a simple web server that handles requests and sends responses.


🔧 What Is the Backend?

The backend is the part of a web application that handles everything behind the scenes — data storage, business logic, user authentication, API communication, and so on. It’s what powers the user experience on the frontend (the part you see and interact with in the browser).


🖥️ What Is a Server?

A server is a program that runs continuously and listens for requests from clients (like browsers or mobile apps). When a request comes in, the server figures out what to do and sends back a response.

Think of it like a waiter at a restaurant:

  • You (the client) place an order (request),

  • The waiter (server) takes it to the kitchen (backend logic),

  • Then returns with your food (response).

In Express.js, we create a server like this:

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

Here, app is our server instance.


📞 What Is server.listen()?

The listen() function tells the server to start running and wait for incoming requests on a specific port.

Example:

app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});

This starts your server on port 3000. You can now access it in your browser at http://localhost:3000.


🏠 What Does "/" Mean?

In web development, "/" is called the home route or root path. It represents the main page of your server.

Example:

app.get('/', (req, res) => {
  res.send('Welcome to the Home Page!');
});

When someone visits http://localhost:3000/, this route is triggered, and the server responds with "Welcome to the Home Page!".


📬 What Is a Request?

A request is what the client (browser/app) sends to the server. It asks the server to do something—like give data, save data, delete data, etc.

A GET request is used when the client wants to retrieve something from the server (like a web page or a list of items).

In Express, we handle GET requests using app.get().

Example:

app.get('/about', (req, res) => {
  res.send('This is the About Page.');
});

This sets up a route for http://localhost:3000/about.

🧾 What Are req and res?

In any Express route like this:

app.get('/', (req, res) => {
  res.send('Welcome to the Home Page!');
});

You see two parameters: req and res. These stand for:

  • req = request

  • res = response

Let’s break down what they do:


req (The Request Object)

The req object contains all the information that the client sends to the server.

It includes things like:

  • Headers – info about the request (like browser type)

  • Query parameters – e.g., /search?query=book

  • Route parameters – e.g., /user/:id

  • Body – data sent in a POST request (like login form data)

  • Method – type of request: GET, POST, etc.

Example: if a user goes to:

http://localhost:3000/greet?name=Alex

You could get their name like this:

app.get('/greet', (req, res) => {
  const userName = req.query.name;
  res.send(`Hello, ${userName}!`);
});

📦 res (The Response Object)

The res object is how you send a response back to the client.

It lets you:

  • Send plain text with res.send()

  • Send HTML

  • Send JSON data with res.json()

  • Set status codes like res.status(404)

Example:

app.get('/', (req, res) => {
  res.send('This is a response from the server.');
});

You can also send JSON:

app.get('/user', (req, res) => {
  res.json({ name: 'John', age: 25 });
});

💡 Quick Analogy

  • req is like a question the user asks the server.

  • res is the answer the server sends back.


Summary

TermMeaningUsage Example
reqRequest from the clientreq.query, req.params, req.body
resResponse the server sends to the clientres.send(), res.json(), res.status()

📦 What Are Dependencies?

Dependencies are third-party libraries or packages your project needs to run. Express itself is a dependency.

We use npm (Node Package Manager) to install dependencies.

Example:

  1. Initialize your project:
npm init -y
  1. Install Express:
npm install express

Now Express is added to your package.json file under "dependencies":

"dependencies": {
  "express": "^4.18.2"
}

🧪 Putting It All Together

Here’s a full example of a simple Express server:

const express = require('express');  // Load the express module
const app = express();              // Create an Express app

// Home Route: GET request to "/"
app.get('/', (req, res) => {
  res.send('Welcome to the Home Page!');
});

// About Route: GET request to "/about"
app.get('/about', (req, res) => {
  res.send('This is the About Page.');
});

// Start the server and listen on port 3000
app.listen(3000, () => {
  console.log('Server is running on http://localhost:3000');
});

🏁 Run the Server

  1. Save the file as server.js.

  2. Run it using Node:

node server.js
  1. Open your browser and go to:


✅ Recap

  • A server waits for and responds to requests from clients.

  • server.listen() starts your server on a chosen port.

  • The "/" route is the home or root route.

  • A GET request is used to fetch data or pages.

  • Dependencies are external packages like Express that you install into your project.


This is your first real step into backend development!

0
Subscribe to my newsletter

Read articles from ADARSH KUMAR PANDEY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ADARSH KUMAR PANDEY
ADARSH KUMAR PANDEY