📖 Scenario: You are building a simple Express API for a small online store. You want to make sure your API works correctly before letting customers use it.
🎯 Goal: Learn how to set up a basic Express API and understand why testing each API endpoint is important to catch errors early and ensure smooth user experience.
📋 What You'll Learn
Create a basic Express server with one GET endpoint
Add a configuration variable for the port number
Implement the main API route that returns a fixed product list
Add a final line to start the server listening on the configured port
💡 Why This Matters
🌍 Real World
APIs are the backbone of modern web apps. Testing them ensures users get correct data and smooth experiences.
💼 Career
Backend developers must build and test APIs to create reliable services that frontend apps and clients depend on.
Progress0 / 4 steps
1
Set up Express and create a basic server
Create a constant called express that requires the 'express' module. Then create a constant called app by calling express().
Express
Hint
Use require('express') to import Express and call it to create your app.
2
Add a configuration variable for the port
Create a constant called PORT and set it to 3000.
Express
Hint
Set PORT to 3000 so your server knows which port to listen on.
3
Create a GET endpoint to return products
Use app.get with the path '/products' and a callback with parameters req and res. Inside the callback, send a JSON response with an array of two products: { id: 1, name: 'Book' } and { id: 2, name: 'Pen' }.
Express
Hint
Use app.get to create a route and res.json to send the product list.
4
Start the server listening on the port
Call app.listen with PORT and a callback function that logs 'Server running on port 3000'.
Express
Hint
Use app.listen to start your server and log a message when ready.
Practice
(1/5)
1. Why is testing APIs important in Express applications?
easy
A. It automatically updates the API documentation.
B. It makes the app run faster on the server.
C. It helps find and fix problems early before users see them.
D. It reduces the size of the API code.
Solution
Step 1: Understand the purpose of API testing
Testing APIs helps catch bugs and errors early in development.
Step 2: Identify the benefit of early problem detection
Finding problems early prevents issues from reaching users and causing failures.
Final Answer:
It helps find and fix problems early before users see them. -> Option C
Quick Check:
API testing = early bug detection [OK]
Hint: Testing finds bugs early to avoid user issues [OK]
Common Mistakes:
Thinking testing speeds up server runtime
Confusing testing with documentation generation
Believing testing reduces code size
2. Which of the following is the correct way to write a simple test for an Express API endpoint using a testing library like Jest and Supertest?
easy
A. app.get('/api').expect(200);
B. request(app).get('/api').expect(200).end(done);
C. request('/api').get(app).expect(200);
D. expect(request(app).get('/api')).toBe(200);
Solution
Step 1: Recognize the correct testing syntax
Using Supertest, you call request(app).get('/api') to test the endpoint.
Step 2: Confirm chaining with expect and end
The correct pattern chains .expect(200) and .end(done) to complete the test.
Final Answer:
request(app).get('/api').expect(200).end(done); -> Option B