Practice & Assessment
Test your understanding of Building RESTful APIs
Multiple Choice Questions
5Which of the following URLs follows REST naming conventions?
Where must the global error handler middleware be registered in an Express app?
What happens in Express 4 when an async route handler throws an error without try/catch?
What should the `meta` field in a paginated response include at minimum?
What is the purpose of `err.isOperational = true` in a custom AppError class?
Coding Challenges
1Products API with Pagination and Error Handling
Build a /products REST API endpoint. GET /products should support ?page, ?limit (max 20), ?sort (by price or name), and ?category filtering. Include a meta object in the response with page, limit, total, and totalPages. GET /products/:id should return 404 with { success: false, error: { message, code } } if not found. Wrap all handlers with an asyncHandler utility. Use an in-memory array of at least 30 products. Inputs: query params page/limit/sort/category. Outputs: { success, data, meta } envelope. Time estimate: 25-30 minutes.
Mini Project
Task Manager REST API
Build a complete Task Manager REST API with consistent response envelopes. Resources: tasks (id, title, description, status: todo/in-progress/done, priority: low/medium/high, createdAt). Endpoints: GET /api/tasks (with ?status, ?priority filters and pagination), POST /api/tasks (validate title required), GET /api/tasks/:id, PUT /api/tasks/:id, DELETE /api/tasks/:id. Implement: custom AppError class, global error handler as last middleware, asyncHandler wrapper on all handlers, consistent { success, data, meta } envelope from a response helper. All errors use { success: false, error: { message, code } } format.
