Express JS Express REST API Development 2 — Questions and Answers
Question 1: What is API versioning in Express and why is it important?
- Prefixing routes with /v1, /v2 to support multiple API versions simultaneously (Correct answer)
- Versioning the Express framework itself
- Tracking database schema versions
- Using semantic versioning in package.json
Correct answer: Prefixing routes with /v1, /v2 to support multiple API versions simultaneously
API versioning allows backward-compatible changes by maintaining multiple route prefixes so old clients aren't broken.
Question 2: How do you validate request body data in an Express REST API?
- Use validation middleware like express-validator or joi before the route handler (Correct answer)
- Validate inside the database layer only
- Use built-in Express validation
- Rely on TypeScript types at runtime
Correct answer: Use validation middleware like express-validator or joi before the route handler
Middleware libraries like express-validator or joi validate and sanitize incoming data before it reaches business logic.
Question 3: What is the purpose of pagination in a REST API endpoint that returns a list of resources?
- To limit the number of items returned per request and reduce payload size (Correct answer)
- To sort results alphabetically
- To cache results between requests
- To filter results by date
Correct answer: To limit the number of items returned per request and reduce payload size
Pagination divides large datasets into manageable pages, improving performance and reducing data transfer.
Question 4: Which Express pattern is used to build a CRUD API for a 'users' resource with standard REST routes?
- GET /users, POST /users, GET /users/:id, PUT /users/:id, DELETE /users/:id (Correct answer)
- GET /users/all, POST /users/create, GET /users/get/:id
- GET /getUsers, POST /createUser, DELETE /removeUser/:id
- GET /api?resource=users&action=list
Correct answer: GET /users, POST /users, GET /users/:id, PUT /users/:id, DELETE /users/:id
RESTful conventions use noun-based plural paths with HTTP methods to express CRUD operations.
Question 5: What does HATEOAS stand for and how does it relate to Express REST APIs?
- Hypermedia As The Engine Of Application State — including links in responses to guide clients (Correct answer)
- HTML And Text Engine Of Application Services
- Hyperlink Access To External API Schemas
- HTTP Access To External Object Storage
Correct answer: Hypermedia As The Engine Of Application State — including links in responses to guide clients
HATEOAS is a REST constraint where responses include links that describe available next actions, making APIs self-discoverable.
Question 6: How do you implement rate limiting in an Express REST API?
- Use express-rate-limit middleware to restrict requests per IP in a time window (Correct answer)
- Use app.throttle()
- Set limits in the nginx configuration only
- Use built-in Express rate limiting
Correct answer: Use express-rate-limit middleware to restrict requests per IP in a time window
express-rate-limit is the standard middleware for capping requests per IP over a configurable time window.
What is API versioning in Express and why is it important?