Node.js and Express.js
Module Summary
This module introduces learners to backend development with Node.js and Express.js, one of the most popular combinations for building web servers and APIs. Starting from the ground up, learners will explore the fundamentals of Node.js, create servers, handle web traffic through routing and middleware, and interact with external APIs to fetch and manage data. This module assumes no prior backend experience and focuses on building a strong foundation for server-side development.
Learning Objectives
By the end of this module, learners will be able to:
- Explain the roles of Node.js and Express.js in web development.
- Set up a Node.js project and manage dependencies using npm.
- Create a web server using Express.js.
- Implement routing to handle different HTTP requests (GET, POST).
- Use middleware for logging, data parsing, and other cross-cutting concerns.
- Fetch and manipulate data from external APIs using both
fetch
andaxios
. - Understand and use route parameters and query strings to create dynamic routes.
- Structure an Express application for better organization and maintainability.
Learning Outcomes
Upon completing this module, learners will be able to build a functional backend server with Node.js and Express.js that can serve static files, define a RESTful API, and communicate with other services.
Module Outline
Lesson 1: Introduction to Node.js & npm
- Time: 2.5 hours
- Topics:
- What is Node.js?
- The Node.js Runtime Environment & Event Loop
npm
andpackage.json
- Modules (
require
/module.exports
)
- Activities:
- Initialize a new
npm
project. - Install external packages like
express
. - Create and use local modules to share code between files.
- Initialize a new
- Knowledge Check: Multiple-choice questions on Node.js concepts, npm commands, and module syntax.
Lesson 2: Your First Express Server
- Time: 2.5 hours
- Topics:
- What is Express.js?
- Creating a server with
express()
- Handling requests and responses (
req
,res
) - Basic routing with
app.get()
- Serving static files with
express.static()
- Activities:
- Build a simple “Hello World” server.
- Create a server that serves a static
index.html
file.
- Knowledge Check: Multiple-choice questions on creating an Express server and serving files.
Lab 1: Build a Basic Express.js Server
- Time: 1.5 hours (asynchronous)
- Task: You have been asked to build a basic server for a local coffee shop to serve their homepage and contact page.
- Objective: By the end of this activity, you will have demonstrated your ability to create a basic Express.js server that serves multiple static HTML files.
Lesson 3: Express Routing
- Time: 2.5 hours
- Topics:
GET
vs.POST
requests- Route parameters (
req.params
) - Query strings (
req.query
) - Differentiating between
req.params
,req.query
, andreq.body
.
- Activities:
- Build an API with dynamic routes (e.g.,
/users/:id
). - Use Postman to test
GET
andPOST
requests with query strings and request bodies.
- Build an API with dynamic routes (e.g.,
- Knowledge Check: Multiple-choice questions on routing, parameters, and queries.
Lesson 4: Express Middleware
- Time: 2.5 hours
- Topics:
- What is middleware?
- The
next()
function - Built-in middleware (
express.json
,express.urlencoded
) - Creating custom middleware
- Activities:
- Add a request logger middleware to an application.
- Create a middleware to validate incoming data or check for an API key.
- Knowledge Check: Multiple-choice questions on the purpose and implementation of middleware.
Lesson 5: Server-Side API Consumption with fetch
- Time: 2.5 hours
- Topics:
- The role of a server as an API client (proxies and facades).
- Integrating
fetch
into Express route handlers for server-to-server communication. - Translating external API errors into meaningful server responses (e.g., 404 to 404, 500 to 502).
- Transforming and sanitizing data from external APIs before sending to a client.
- Activities:
- Build a “proxy route” that fetches data, transforms it, and serves it to the client.
- Implement robust error handling for failed or non-ok API requests.
- Knowledge Check: Questions on server-side error handling, data transformation, and the flow of data from an external API through an Express server.
Lesson 6: Advanced API Interactions with axios
- Time: 2.5 hours
- Topics:
- Advantages of
axios
overfetch
. - Making
GET
andPOST
requests withaxios
. - Creating configured instances with
axios.create()
. - Using interceptors to modify requests and responses.
- Advantages of
- Activities:
- Refactor a
fetch
request to useaxios
. - Create a configured
axios
instance for an API.
- Refactor a
- Knowledge Check: Questions on
axios
instances and interceptors.
Lab 2: Server to Server Communication
- Time: 1.5 hours (asynchronous)
- Task: You are building a server for a local coffee shop. The shop wants to display live data from a public API on their homepage.
- Objective: By the end of this activity, you will demonstrate the ability to create a server that can fetch data from an external API and serve it to the client.
Lesson 7: Project Structure & Configuration
- Time: 2 hours
- Topics:
- The “separation of concerns” principle.
- Structuring an Express app (routes, controllers, services).
- Securely managing configuration and secrets with
dotenv
. - Understanding the role of
.gitignore
.
- Activities:
- Refactor a simple server into a routes/controllers structure.
- Externalize configuration values into a
.env
file.
- Knowledge Check: Questions on project structure and secret management best practices.
Final Assessments
Skills-Based Assessment (SBA)
- Time: 4 hours
- Format: Practical project
- Activity: Build a RESTful API from scratch. The API will need to interact with an external data source, include multiple routes with data validation, and be well-structured.
- Deliverable: Submit a complete and functional Express.js project via GitHub.
- Rubric: Evaluation based on code quality, functionality, proper API implementation, error handling, and project structure.
Knowledge-Based Assessment (KBA)
- Time: 1.5 hours (asynchronous)
- Format: Multiple-choice and short-answer questions covering all major topics from the module, including Node.js, Express, routing, middleware, and API consumption.