Unit 1 of 3 · BCA Sem 4

Unit 1: Node.js backend foundations

Web Programming-II notes · PTU syllabus (UGDSE303)

3 min read4 topics8 exam questions
On this page
  1. Unit summary
  2. Node.js basics
  3. Asynchronous programming
  4. Express.js: server, routing and middleware
  5. RESTful API design and CRUD
  6. Key terms
  7. Quick revision
  8. Important questions

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
ComparisonCallbacks vs promises vs async/await
Style
Readability

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

1

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 in package.json.
  • Modules: built-in (fs, http, path), local (require("./math") or ES import) and third-party (from npm).
  • REPL: an interactive shell (type node) for testing code.
  • File system: fs.readFile, fs.writeFile, fs.appendFile and their promise versions.
2

Topic 2

Asynchronous programming

ComparisonCallbacks vs promises vs async/await
Style
Drawback

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);
  }
}
3

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.

4

Topic 4

RESTful API design and CRUD

ActionHTTP methodRouteStatus
List tasksGET/api/tasks200
Get one taskGET/api/tasks/:id200 or 404
Create a taskPOST/api/tasks201
Update a taskPUT / PATCH/api/tasks/:id200
Delete a taskDELETE/api/tasks/:id204

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

  1. Q1.What is Node.js and why is it non-blocking?
  2. Q2.What is npm?
  3. Q3.Differentiate between callbacks and promises.
  4. Q4.What is middleware in Express?
  5. Q5.What is Postman used for?

Long-answer questions

  1. Q1.Explain asynchronous programming in Node.js with callbacks, promises and async/await.
  2. Q2.Build a REST API for tasks using Express with all CRUD routes.
  3. 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.

WhatsApp us