📖 Scenario: You are building a simple Express server that provides information about books. Good API documentation helps other developers understand how to use your server endpoints correctly.
🎯 Goal: Create a basic Express server with a single endpoint /books that returns a list of books. Add clear comments to document the API endpoint, explaining what it does and how to use it.
📋 What You'll Learn
Create an Express app with a /books GET endpoint
Define a list of books as an array of objects with id and title
Add a configuration variable for the port number
Add comments above the endpoint to document its purpose and usage
Start the server listening on the configured port
💡 Why This Matters
🌍 Real World
API documentation helps developers understand how to use your server endpoints without confusion, saving time and reducing mistakes.
💼 Career
Clear API documentation is a key skill for backend developers and anyone working with web services or APIs.
Progress0 / 4 steps
1
DATA SETUP: Create the books data array
Create a constant array called books with these exact objects: { id: 1, title: '1984' }, { id: 2, title: 'Brave New World' }, and { id: 3, title: 'Fahrenheit 451' }.
Express
Hint
Use const books = [ ... ] with three objects inside.
2
CONFIGURATION: Set the server port
Create a constant called PORT and set it to 3000.
Express
Hint
Use const PORT = 3000; to set the port.
3
CORE LOGIC: Create the Express app and the /books endpoint
Import Express, create an app with express(), and add a GET endpoint /books that sends the books array as JSON.
Express
Hint
Use const express = require('express'); and const app = express();. Then add app.get('/books', (req, res) => { res.json(books); });.
4
COMPLETION: Add API documentation comments and start the server
Add a comment above the /books endpoint explaining it returns a list of books as JSON. Then add app.listen(PORT) to start the server.
Express
Hint
Add a comment like // GET /books endpoint returns a list of books as JSON above the endpoint. Then add app.listen(PORT); below.
Practice
(1/5)
1. Why is API documentation important when building an Express app?
easy
A. It helps users understand how to use the API correctly.
B. It makes the app run faster.
C. It automatically fixes bugs in the code.
D. It hides the API from users.
Solution
Step 1: Understand the purpose of API documentation
API documentation explains how to use the API, making it easier for users to understand its functions.
Step 2: Identify the correct benefit
Helping users understand usage prevents mistakes and confusion, which is the main goal of documentation.
Final Answer:
It helps users understand how to use the API correctly. -> Option A
Quick Check:
API documentation = clear usage guide [OK]
Hint: Documentation explains usage clearly to avoid confusion [OK]