🚀 Day 11: Introduction to Express.js & REST API in Node.js

Table of contents
- 👨🏫 What You’ll Learn Today
- 🧾 What is Express.js?
- 📦 Why Use Express.js?
- 🛠️ How to Install Express.js
- ✏️ Let’s Build a Simple REST API using Express.js
- 💻 Code: index.js (Full Example with Explanation)
- ▶️ How to Run the Project
- 🧠 Error Handling Examples in Code
- ❓ FAQs – Frequently Asked Questions
- ❗Problem: "I am not getting output in browser for POST, PUT, DELETE"
- ✅ Simple Explanation
- ✅ What Should You Use Instead?
- ✅ Summary
- 📝 Practice for Students
- ✅ Summary
- 🔔 Stay Connected
👨🏫 What You’ll Learn Today
What is Express.js?
Why use Express in Node.js?
How to install and set up Express
What is a REST API?
Real-life mini project: Creating a basic REST API
HTTP methods: GET, POST, PUT, DELETE
FAQs
🧾 What is Express.js?
Express.js is a fast, minimal, and flexible Node.js framework that makes it easier to build web servers and APIs.
Without Express: You manually handle
http.createServer()
, headers, routes
With Express: You just write simple and clean route handlers
✅ Think of Express like a shortcut to build backend apps quickly.
📦 Why Use Express.js?
Without Express (Node only) | With Express |
More boilerplate code | Less code, more readability |
Manual routing | Easy .get() , .post() methods |
Manual status codes, headers | Handled automatically |
No middleware support | Built-in middleware system |
🛠️ How to Install Express.js
Step 1: Create a new folder
mkdir express-intro
cd express-intro
Step 2: Initialize a Node project
npm init -y
Step 3: Install Express
npm install express
✏️ Let’s Build a Simple REST API using Express.js
🌍 Project Idea: Manage a list of students
You’ll build a REST API that can:
Add a student
Get all students
Update a student
Delete a student
📁 Folder Structure
express-intro/
├── index.js <-- main file
🧠 What is REST API?
REST stands for: REpresentational State Transfer
A REST API uses HTTP methods to perform actions on data:
HTTP Method | Use For |
GET | Read data |
POST | Create data |
PUT | Update entire data |
PATCH | Update partially |
DELETE | Delete data |
💻 Code: index.js (Full Example with Explanation)
// index.js
const express = require("express");
const app = express();
const port = 5000;
// In-memory database (array)
let students = [
{ id: 1, name: "Payal" },
{ id: 2, name: "Amit" },
];
// Middleware to parse JSON body
app.use(express.json());
// ✅ Route: GET all students
app.get("/students", (req, res) => {
res.status(200).json(students);
});
// ✅ Route: POST - add new student
app.post("/students", (req, res) => {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: "Name is required" });
}
const newStudent = {
id: students.length + 1,
name,
};
students.push(newStudent);
res.status(201).json(newStudent);
});
// ✅ Route: PUT - update student
app.put("/students/:id", (req, res) => {
const { id } = req.params;
const { name } = req.body;
const student = students.find((s) => s.id == id);
if (!student) {
return res.status(404).json({ error: "Student not found" });
}
student.name = name;
res.json(student);
});
// ✅ Route: DELETE - remove student
app.delete("/students/:id", (req, res) => {
const { id } = req.params;
const index = students.findIndex((s) => s.id == id);
if (index === -1) {
return res.status(404).json({ error: "Student not found" });
}
students.splice(index, 1);
res.json({ message: "Student deleted successfully" });
});
// Start the server
app.listen(port, () => {
console.log(`🚀 Server is running at http://localhost:${port}`);
});
▶️ How to Run the Project
node index.js
Now open browser or use Postman:
GET
http://localhost:5000/students
→ Get all studentsPOST
http://localhost:5000/students
→ body:{ "name": "Ravi" }
PUT
http://localhost:5000/students/1
→ body:{ "name": "Updated Name" }
🧠 Error Handling Examples in Code
Case | How It’s Handled |
Empty name in POST | Returns 400 Bad Request |
Non-existent student in PUT/DELETE | Returns 404 Not Found |
Server crash? | Caught with middleware (coming later) |
❓ FAQs – Frequently Asked Questions
1) Do I need Express for every project?
Not always. If you're only doing file read/write, plain Node is okay.
But for building APIs, Express saves a lot of time.
2) What is app.use(express.json())
?
It allows Express to understand JSON
data sent in body of request.
Without this, req.body
will be undefined
.
3) Can we connect this to a database?
Yes! We’re using an array for demo. In the next lessons, we’ll connect to MongoDB using Mongoose.
4) What if I call PUT
with a wrong ID?
Our code checks if student exists. If not, it returns:
{ "error": "Student not found" }
5) Is this RESTful API?
✅ Yes. Each route follows REST principles:GET
, POST
, PUT
, DELETE
on the resource /students
.
❗Problem: "I am not getting output in browser for POST, PUT, DELETE"
You're trying to perform:
POST
PUT
DELETE
directly from the browser URL bar, but browser can only make GET
requests from the address bar.
✅ Simple Explanation
💻 What you can use the browser for:
Method | Can you use it directly in browser? | Example |
GET | ✅ Yes | http://localhost:5000/students |
POST | ❌ No | Need Postman or frontend form |
PUT | ❌ No | Need Postman or frontend app |
DELETE | ❌ No | Need Postman or frontend app |
✅ What Should You Use Instead?
Use one of the following tools:
1️⃣ Postman (Recommended for students and professionals)
Download: https://www.postman.com/downloads/
Create requests:
Choose method:
POST
,PUT
,DELETE
URL:
http://localhost:5000/students
Set
Body
>raw
>JSON
Example body:
{ "name": "Ravi" }
2️⃣ Thunder Client (VS Code Extension)
In VS Code, install "Thunder Client"
Works just like Postman inside your code editor
3️⃣ Build a Simple Frontend using HTML/JS to send fetch()
requests
If you want, I can give you a very simple HTML frontend with buttons for:
Add Student
Edit Student
Delete Student
View All Students
Just let me know.
✅ Summary
Request Method | Can run in browser URL bar? | What to use instead? |
GET | ✅ Yes | Browser / Postman / Code |
POST | ❌ No | Postman / Thunder / Frontend |
PUT | ❌ No | Postman / Thunder / Frontend |
DELETE | ❌ No | Postman / Thunder / Frontend |
📝 Practice for Students
Add a new route:
GET /students/:id
Add email along with name in student object
Add validation for email format
Create a separate
routes
folder and move routes there (structure practice)
✅ Summary
Topic | Summary |
Express.js | Lightweight Node.js framework |
REST API | Uses HTTP methods to manage data |
Routes | app.get , app.post , etc. |
Body parsing | express.json() |
Error handling | Simple conditions + status codes |
🔔 Stay Connected
If you found this article helpful and want to receive more such beginner-friendly and industry-relevant Node JS notes, tutorials, and project ideas — 📩 Subscribe to our newsletter by entering your email below.
And if you're someone who wants to prepare for tech interviews while having a little fun and entertainment, 🎥 Don’t forget to subscribe to my YouTube channel – Knowledge Factory 22 – for regular content on tech concepts, career tips, and coding insights!
Stay curious. Keep building. 🚀
Subscribe to my newsletter
Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Porwal
Payal Porwal
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: 🚀 In-depth explorations of emerging technologies 💡 Practical tutorials and how-to guides 🔧Insights on software development best practices 🚀Reviews of the latest tools and frameworks 💡 Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Let’s connect and grow together! 🌟