Skip to content

kamlakantkumar51/Express

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Express.js Complete Learning Repository

A comprehensive Express.js learning repository covering everything from basic server creation to advanced backend concepts. This repository serves as both a revision guide and hands-on practice resource for building REST APIs and backend applications using Express.js.


πŸ“Œ Overview

This repository contains various Express.js examples and exercises that demonstrate:

  • Creating Express Servers
  • Routing
  • Route Parameters
  • Query Strings
  • Request & Response Objects
  • Middleware
  • REST APIs
  • HTTP Methods
  • Error Handling
  • Express Architecture
  • Backend Development Fundamentals

The goal of this repository is to learn Express.js from Beginner β†’ Intermediate β†’ Advanced level through practical examples.


πŸ›  Tech Stack

  • Node.js
  • Express.js
  • JavaScript (ES6+)
  • REST APIs

πŸ“‚ Project Structure

Express/
β”‚
β”œβ”€β”€ app.js
β”œβ”€β”€ package.json
β”œβ”€β”€ routes/
β”œβ”€β”€ middleware/
β”œβ”€β”€ controllers/
β”œβ”€β”€ public/
β”œβ”€β”€ views/
└── README.md

⚑ Getting Started

Clone Repository

git clone <repository-url>

Navigate to Project

cd Express

Install Dependencies

npm install

Start Server

node app.js

or

nodemon app.js

🌐 Creating an Express Server

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

let port = 8080;

app.listen(port, () => {
    console.log(`App is listening on port ${port}`);
});

Explanation

  • express() creates an Express application.
  • app.listen() starts the server.
  • Server listens for incoming requests on specified port.

πŸ”„ Request-Response Cycle

Every Express application follows:

Client Request
      ↓
Express Server
      ↓
Route Handler
      ↓
Response Sent

Example:

app.get("/", (req, res) => {
    res.send("Hello Express");
});

πŸ›£ Routing in Express

Routing determines how an application responds to a specific client request.

app.get("/", (req, res) => {
    res.send("Home Page");
});

🎯 Route Parameters

Route Parameters allow dynamic URLs.

Example:

app.get("/:username/:id", (req, res) => {
    let { username, id } = req.params;

    let htmlStr =
        `<h1>Welcome to the page of ${username}</h1>`;

    res.send(htmlStr);
});

URL Example

http://localhost:8080/kamlakant/101

Output

Welcome to the page of kamlakant

πŸ” Query Strings

Query parameters are used for searching and filtering data.

Example:

app.get("/search", (req, res) => {
    console.log(req.query);
    res.send("No Result");
});

URL

http://localhost:8080/search?q=apple

req.query

{
    q: "apple"
}

πŸ“₯ Sending Responses

Plain Text

res.send("Hello World");

HTML Response

res.send("<h1>Hello World</h1>");

Dynamic HTML

let username = "Kamlakant";

res.send(
    `<h1>Welcome ${username}</h1>`
);

πŸ“€ Request Object (req)

The Request Object contains information sent by client.

Common Properties:

req.params
req.query
req.body
req.headers
req.method
req.url

Example:

console.log(req.query);
console.log(req.params);

πŸ“¨ Response Object (res)

Used to send data back to client.

Methods:

res.send()
res.json()
res.render()
res.redirect()
res.status()

Example:

res.send("Success");

🌍 Dynamic Routing

Instead of creating multiple routes:

❌ Bad

app.get("/user1");
app.get("/user2");
app.get("/user3");

βœ… Better

app.get("/:username", (req, res) => {
    res.send(req.params.username);
});

🚦 HTTP Methods

GET

Used to retrieve data.

app.get("/", (req, res) => {
    res.send("GET Request");
});

POST

Used to create data.

app.post("/", (req, res) => {
    res.send("POST Request");
});

PUT

Used to update data.

app.put("/", (req, res) => {
    res.send("PUT Request");
});

DELETE

Used to remove data.

app.delete("/", (req, res) => {
    res.send("DELETE Request");
});

βš™ Middleware

Middleware functions execute before route handlers.

Example:

app.use((req, res, next) => {
    console.log("Middleware Executed");
    next();
});

Uses

  • Authentication
  • Logging
  • Validation
  • Error Handling

🚫 404 Handling

Catch all unmatched routes.

app.get("*", (req, res) => {
    res.send("404 Page Not Found");
});

πŸ“Š Express Application Flow

Request
   ↓
Middleware
   ↓
Route Matching
   ↓
Controller Logic
   ↓
Response

πŸŽ“ Concepts Covered

Beginner

  • Express Installation
  • Creating Server
  • Routing
  • Request & Response
  • Route Parameters
  • Query Strings

Intermediate

  • Middleware
  • REST APIs
  • Dynamic Routes
  • HTTP Methods
  • Error Handling

Advanced

  • MVC Architecture
  • Authentication
  • Authorization
  • JWT
  • Database Integration
  • File Upload
  • Session Management
  • Deployment

🧠 Important Interview Questions

What is Express.js?

Express.js is a lightweight Node.js web framework used for building web applications and APIs.


What is Middleware?

Functions that execute between request and response cycle.


Difference Between req.params and req.query?

req.params

/user/kamal
req.params.username

req.query

/search?q=apple
req.query.q

What is Routing?

Routing determines how the application responds to a specific URL request.


What is REST API?

An API following REST principles using HTTP methods.


What is app.use()?

Used to register middleware functions.


πŸš€ Future Improvements

  • MongoDB Integration
  • Mongoose Models
  • JWT Authentication
  • MVC Pattern
  • RESTful APIs
  • CRUD Operations
  • File Uploads
  • Cloudinary Integration
  • Session Authentication
  • Deployment on Render

πŸ“š Learning Outcome

After completing this repository, you will understand:

βœ… Express Server Setup

βœ… Routing

βœ… Request & Response Handling

βœ… Dynamic URLs

βœ… Query Parameters

βœ… Middleware

βœ… HTTP Methods

βœ… REST APIs

βœ… Backend Fundamentals

βœ… Scalable Express Project Structure


πŸ‘¨β€πŸ’» Author

Kamlakant Kumar

Aspiring Software Engineer | MERN Stack Developer | Java Enthusiast | DSA Practitioner


⭐ If you found this repository useful, consider giving it a star!

About

A structured Express.js repository featuring hands-on examples of server development, API building, middleware, routing, and modern backend best practices.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors