Practice & Assessment
Test your understanding of Express.js: Building REST APIs
Multiple Choice Questions
5What happens if an Express middleware does not call next() and does not send a response?
In Express 4, why does throwing an error inside an async route handler not reach the error middleware?
What does Zod's safeParse return when validation fails?
What is the correct signature for an Express error-handling middleware?
Why should multer's fileFilter reject files based on mimetype rather than file extension?
Coding Challenges
1Validated REST API for a Todo List
Build an Express REST API with in-memory storage for a todo list. Implement GET /todos (list all), POST /todos (body: {title: string min 1 char, completed: boolean optional default false}), PATCH /todos/:id (partial update), DELETE /todos/:id. Use Zod for all input validation and return 400 with field errors on invalid input. Return 404 with a clear message for unknown IDs. Add a request logger middleware that prints method, path, and response status. Input: none. Output: working CRUD API testable with curl or Postman. Estimated time: 30 minutes.
Mini Project
Image Upload and Gallery API
Build an Express API for uploading and retrieving images. Requirements: POST /api/images — accept multipart upload with field name photo, validate file is jpeg/png/webp, enforce 5MB limit, store with UUID filename in an uploads/ directory, return {id, filename, size, url}. GET /api/images — return array of all uploaded images with metadata read from the uploads/ directory. GET /api/images/:id — return image metadata. GET /uploads/:filename — serve the actual image file. DELETE /api/images/:id — delete the file and return 204. Implement a global error handler that catches multer errors (file too large, wrong type) and returns 400 with a descriptive message.
