Task 1: Set Up Express Project
- Create a new folder called
express-practice - Initialise a new Node.js project with
npm init -y - Install Express and TypeScript dependencies:
npm install express dotenv npm install --save-dev typescript @types/node @types/express tsx - Create a
tsconfig.jsonfile with the settings from this lesson - Create a
srcfolder andindex.tsfile - Set up environment variables with a
.envfile - Create the basic Express server that runs on the port from your
.envfile - Add the scripts to
package.jsonfor development and production
Task 2: Create Game Routes
Set up an array of games with a proper TypeScript interface. Each game should have: id, title, genre, releaseYear, price, and rating.
Add these routes to your Express server:
GET /games- Return an array of 6 gamesGET /games/:id- Return a single game by ID, with 404 error if not foundGET /games/genre/:genre- Return all games in a specific genre
Task 3: Add Query Parameters
Add these features to your game routes:
GET /games?maxPrice=50- Filter games by maximum priceGET /games?genre=action&maxPrice=60- Combine genre and price filtersGET /games?sort=rating- Sort games by rating (highest first)GET /games?year=2023- Filter games by release year
Task 4: Create Movie Routes
Set up an array of movies with a proper TypeScript interface. Each movie should have: id, title, director, year, genre, and duration.
Add these routes for managing movies:
GET /movies- Return an array of 4 moviesGET /movies/:id- Return a single movie by ID, with 404 error if not foundGET /movies?minYear=2020- Filter movies by minimum yearGET /movies?search=spider- Search movies by title (case-insensitive)GET /movies?genre=comedy&maxDuration=120- Filter by genre and maximum duration
Task 5: Error Handling Practice
Add proper error handling to all your routes:
- Return 400 for invalid IDs (non-numeric)
- Return 404 for resources not found
- Add validation for query parameters (e.g., maxPrice must be a number, year must be between 1900-2024)