This assignment focuses on building a RESTful API using Express.js, implementing proper routing, middleware, and error handling.
You will:
- Set up an Express.js server
- Create RESTful API routes for a product resource
- Implement custom middleware for logging, authentication, and validation
- Add comprehensive error handling
- Develop advanced features like filtering, pagination, and search
- Accept the GitHub Classroom assignment invitation
- Clone your personal repository that was created by GitHub Classroom
- Install dependencies:
npm install - Run the server:
# create a .env file from .env.example and (optionally) set API_KEY node server.js
Week2-Assignment.md: Detailed assignment instructionsserver.js: Starter Express.js server file.env.example: Example environment variables file
- Node.js (v18 or higher)
- npm or yarn
- Postman, Insomnia, or curl for API testing
The API will have the following endpoints:
GET /api/products: Get all productsGET /api/products/:id: Get a specific productPOST /api/products: Create a new productPUT /api/products/:id: Update a productDELETE /api/products/:id: Delete a product
Additional features implemented:
- Filtering by category:
GET /api/products?category=electronics - Pagination:
GET /api/products?page=2&limit=5 - Search (query):
GET /api/products?search=phone - Explicit search endpoint:
GET /api/products/search?name=phone - Statistics:
GET /api/products/stats(returns total, avgPrice, countByCategory)
Authentication:
- All
/apiroutes require an API key provided via thex-api-keyheader (orAuthorization). - Default API key:
secret-key(change in your.envfile by settingAPI_KEY).
Sample requests (using curl):
# List products (with API key)
curl -H "x-api-key: secret-key" http://localhost:3000/api/products
# Create a product
curl -X POST -H "Content-Type: application/json" -H "x-api-key: secret-key" \
-d '{"name":"Table","description":"Wooden dining table","price":250,"category":"furniture","inStock":true}' \
http://localhost:3000/api/productsYour work will be automatically submitted when you push to your GitHub Classroom repository. Make sure to:
- Complete all the required API endpoints
- Implement the middleware and error handling
- Document your API in the README.md
- Include examples of requests and responses