Unit 1: Node.js backend foundations
Web Programming-II notes · PTU syllabus (UGDSE303)
On this page
Unit summary
Full-stack JavaScript lets you build both the server and the browser side of an application in one language. This unit covers Node.js — setup, npm, modules, the REPL and the file system — asynchronous programming, Express.js servers, routing and middleware, RESTful API design tested with Postman, and CRUD with MongoDB or MySQL.
After this unit you can
- Set up Node.js projects with npm and modules
- Write asynchronous code with callbacks, promises and async/await
- Build an Express server with routes and middleware
- Design a REST API and perform CRUD with a database
PTU syllabus topics
- Introduction to full-stack JavaScript
- Node.js setup/npm/modules/REPL/file system
- asynchronous programming (callbacks, promises, async/await)
- Express.js server setup
- routing
- middleware
- HTTP methods
- RESTful API design
- JSON and Postman
- CRUD operations with MongoDB/MySQL
Callbacks
Function passed to run later
Can nest deeply ('callback hell')
Promises
.then() and .catch() chains
Flatter and easier to handle errors
async/await
Write async code like normal code
Most readable
Topic 1
Node.js basics
Node.js is a JavaScript runtime built on Chrome's V8 engine that runs JavaScript on the server. It is single-threaded and event-driven with non-blocking I/O, which makes it efficient for many simultaneous connections.
- npm (Node Package Manager):
npm init -y,npm install express, scripts inpackage.json. - Modules: built-in (
fs,http,path), local (require("./math")or ESimport) and third-party (from npm). - REPL: an interactive shell (type
node) for testing code. - File system:
fs.readFile,fs.writeFile,fs.appendFileand their promise versions.
Topic 2
Asynchronous programming
Callbacks
Pass a function to run when done
Nested "callback hell"
Promises
.then() and .catch() chains
Long chains can still be hard to read
async/await
Write async code that reads like sync code
Must use try/catch for errors
javascriptconst fs = require("fs/promises");
async function readNotes() {
try {
const text = await fs.readFile("notes.txt", "utf8");
console.log(text);
} catch (err) {
console.error("Could not read file", err);
}
}Topic 3
Express.js: server, routing and middleware
javascriptconst express = require("express");
const app = express();
app.use(express.json()); // middleware: parse JSON bodies
app.use((req, res, next) => { // custom logging middleware
console.log(req.method, req.url); next();
});
app.get("/api/tasks", (req, res) => res.json(tasks));
app.post("/api/tasks", (req, res) => { tasks.push(req.body); res.status(201).json(req.body); });
app.put("/api/tasks/:id", (req, res) => { /* update */ });
app.delete("/api/tasks/:id", (req, res) => { /* delete */ });
app.listen(5000, () => console.log("Server on 5000"));Middleware is a function with access to req, res and next that runs between the request and the route handler — used for parsing, logging, authentication and error handling.
Topic 4
RESTful API design and CRUD
| Action | HTTP method | Route | Status |
|---|---|---|---|
| List tasks | GET | /api/tasks | 200 |
| Get one task | GET | /api/tasks/:id | 200 or 404 |
| Create a task | POST | /api/tasks | 201 |
| Update a task | PUT / PATCH | /api/tasks/:id | 200 |
| Delete a task | DELETE | /api/tasks/:id | 204 |
Test each endpoint with Postman: choose the method, URL, headers and JSON body, then check the status and response. Connect to MongoDB with Mongoose (Task.find(), Task.create()) or to MySQL with the mysql2 package.
Key terms
- Node.js
- A runtime for executing JavaScript on the server
- npm
- The package manager for Node.js
- Promise
- An object representing a future result of an async operation
- Middleware
- A function processing requests before the route handler
- REST API
- An API using URLs and HTTP methods on resources
Quick revision
- Node: single-threaded, event-driven, non-blocking.
- async/await is cleaner than callbacks and promise chains.
- app.use for middleware; app.get/post/put/delete for routes.
- POST → 201, DELETE → 204, missing → 404.
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 and why is it non-blocking?
- Q2.What is npm?
- Q3.Differentiate between callbacks and promises.
- Q4.What is middleware in Express?
- Q5.What is Postman used for?
Long-answer questions
- Q1.Explain asynchronous programming in Node.js with callbacks, promises and async/await.
- Q2.Build a REST API for tasks using Express with all CRUD routes.
- Q3.Explain middleware in Express with examples.
Stuck on this unit?
Message SBS on WhatsApp for help with Web Programming-II, or to ask about studying BCA at Synetic.
