Unit 3: Backend development and databases
Web Programming-I notes · PTU syllabus (UGDSE301)
On this page
Unit summary
Modern web applications are built as a backend that serves data and a database that stores it. This unit introduces server-side programming with Node.js/Express or PHP, routing GET and POST requests, sessions and cookies, relational and NoSQL databases (MySQL, PostgreSQL, MongoDB), CRUD operations, and JSON-based REST APIs.
After this unit you can
- Explain the role of a backend and create routes with Express.js
- Handle GET and POST requests and manage sessions
- Perform CRUD operations by connecting a backend to a database
- Design and use JSON REST APIs
PTU syllabus topics
- Introduction to server-side programming with Node.js or PHP
- Express.js routing
- handling GET/POST requests
- session and cookie management
- introduction to MySQL/PostgreSQL/MongoDB
- CRUD operations
- connecting backend to database
- JSON and REST APIs
Topic 1
Server-side programming with Node.js and Express
Node.js runs JavaScript on the server; Express.js is a lightweight framework for building web servers and APIs on Node.
javascriptconst express = require("express");
const app = express();
app.use(express.json()); // parse JSON bodies
app.get("/students", (req, res) => { // GET route
res.json([{ roll: 1, name: "Ana" }]);
});
app.post("/students", (req, res) => { // POST route
const s = req.body;
res.status(201).json({ message: "Created", student: s });
});
app.listen(3000);Routing maps a URL and HTTP method to a handler function. Route parameters (/students/:id) read values from the URL.
Example
A minimal Express app: app.get('/hello', (req, res) => res.send('Hello SBS')); followed by app.listen(3000) returns Hello SBS when a browser opens localhost:3000/hello.
Exam tip
Express handles routing, requests and responses on top of Node.js.
Topic 2
Sessions and cookies on the server
Express uses middleware such as express-session and cookie-parser; PHP uses session_start() and $_SESSION. Sessions keep users logged in across requests; cookies store a session ID in the browser.
Example
Why a session ID is needed: HTTP forgets each request. On login the server creates a session and sends its ID to the browser in a cookie. The browser returns that ID with every request, and the server uses it to find the user's data.
Topic 3
Databases: relational and NoSQL
A relational database stores data in tables with fixed columns and uses SQL. A NoSQL database stores data in flexible forms such as documents or key-value pairs and scales easily across many servers.
Structure
Tables with fixed schema
Flexible JSON-like documents
Query language
SQL
Query objects / methods
Relationships
Joins and foreign keys
Embedded documents or references
Best for
Structured, transactional data
Rapidly changing or large unstructured data
Example
Which to choose: a college fee system, where every record has the same fields and totals must always be correct, suits MySQL. A social app, where each post may have different fields such as text, photos or polls and millions of users, suits a document database such as MongoDB.
Topic 4
CRUD operations
CRUD = Create, Read, Update, Delete — the four basic data operations.
| Operation | SQL | HTTP method | MongoDB |
|---|---|---|---|
| Create | INSERT | POST | insertOne() |
| Read | SELECT | GET | find() |
| Update | UPDATE | PUT / PATCH | updateOne() |
| Delete | DELETE | DELETE | deleteOne() |
php<?php
$conn = new mysqli("localhost", "root", "", "college");
$stmt = $conn->prepare("INSERT INTO student (name, course) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $course); // prepared statement prevents SQL injection
$stmt->execute();
?>Example
CRUD as SQL: Create → INSERT INTO student (name) VALUES ('Ana'). Read → SELECT * FROM student. Update → UPDATE student SET name = 'Anu' WHERE roll = 1. Delete → DELETE FROM student WHERE roll = 1.
Topic 5
JSON and REST APIs
JSON is the standard data format for APIs: {"roll": 1, "name": "Ana", "courses": ["BCA"]}. A REST API exposes resources at URLs and uses HTTP methods to act on them, returning JSON and status codes.
- 200 OK
- Request succeeded
- 201 Created
- A new resource was created
- 400 Bad Request
- Invalid input
- 404 Not Found
- Resource does not exist
- 500 Internal Server Error
- Server-side failure
Exam tip
REST principles: resources identified by URLs, standard HTTP methods, stateless requests and JSON representations.
Example
A REST API for students: GET /students lists all; GET /students/12 returns one; POST /students adds one; PUT /students/12 updates it; DELETE /students/12 removes it. In PHP, json_encode($row) turns an array into JSON and json_decode($text, true) turns JSON into an array.
Key terms
- Backend
- The server-side part of an application handling logic and data
- Express.js
- A minimal web framework for Node.js
- Route
- A mapping from URL and method to a handler
- CRUD
- Create, read, update and delete operations
- REST API
- An interface using URLs and HTTP methods to work with resources
Quick revision
- app.get / app.post define routes in Express.
- SQL: tables and joins; NoSQL: flexible documents.
- CRUD ↔ POST, GET, PUT/PATCH, DELETE.
- Use prepared statements to prevent SQL injection.
- 200 OK, 201 Created, 404 Not Found, 500 Server Error.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.What is Node.js?
- Q2.What is routing in Express?
- Q3.Differentiate between SQL and NoSQL databases.
- Q4.Expand CRUD and map it to HTTP methods.
- Q5.What is a REST API?
- Q6.Why are prepared statements used?
Long-answer questions
- Q1.Explain how to build a simple REST API with Express.js for student records.
- Q2.Explain CRUD operations with SQL queries and a PHP-MySQL example.
- Q3.Compare relational and NoSQL databases with examples.
Stuck on this unit?
Message SBS on WhatsApp for help with Web Programming-I, or to ask about studying BCA at Synetic.
