What is Express.js? The "Fast & Minimalist" Framework for Node.js | Lecture 10


Express.js with Node.js 


1. What is Node.js?

Node.js is a runtime environment that allows us to run JavaScript outside the browser.

Why Node.js?

  • Very fast (uses V8 Engine)

  • Non-blocking (handles many users at once)

  • Used to create backend servers, APIs, real-time apps

Example (Simple Node Program)

console.log("Hello from Node.js");

Run using:

node app.js

2. What is Express.js?

Express.js is a framework built on Node.js that makes backend development easy and fast.

Why Express?

  • Less code

  • Easy routing

  • Easy API creation

  • Used in MERN stack

  • Industry standard

 Node.js = Engine
 Express.js = Toolkit

3. Installing Node.js & Express

Step 1: Initialize project

npm init -y

Step 2: Install Express

npm install express

4. Create First Express Server

Basic Server Code

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

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Explanation

  • express() → creates app

  • app.listen() → starts server

  • 3000 → port number

5. Routing in Express

What is Routing?

Routing decides what response to send for a URL.

GET Request Example

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

Another Route

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

  Open in browser:

  • http://localhost:3000/

  • http://localhost:3000/about

6. HTTP Methods in Express

MethodUse
GETGet data
POSTSend data
PUTUpdate data
DELETEDelete data

POST Example

app.post("/login", (req, res) => {
  res.send("Login successful");
});

7. Request (req) and Response (res)

req contains:

  • params

  • query

  • body

  • headers

res is used to send data

app.get("/user", (req, res) => {
  res.send("User Data");
});

8. Query Parameters

URL Example:

/search?name=akash&age=25

Code

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

Output:

{
  "name": "akash",
  "age": "25"
}

9. Route Parameters

URL:

/user/101

Code:

app.get("/user/:id", (req, res) => {
  res.send("User ID is " + req.params.id);
});

10. Middleware in Express

What is Middleware?

Middleware is a function that runs between request and response.

Example

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

 next() allows request to continue

11. Body Parsing (Form / JSON Data)

Install:

npm install body-parser

Use:

const bodyParser = require("body-parser");
app.use(bodyParser.json());

POST with JSON

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

12. Sending JSON Response

app.get("/api", (req, res) => {
  res.json({
    name: "Aakash",
    course: "MERN Stack"
  });
});

13. Status Codes

res.status(200).send("Success");
res.status(404).send("Not Found");
res.status(500).send("Server Error");

14. Serving Static Files

Folder Structure

public/
  index.html

Code

app.use(express.static("public"));


Comments

Popular posts from this blog

Introduction to Node.js | Lecture 1

Introduction to NPM | Lecture 2

Modules in Node.js | Lecture 4