Task 1: Set Up Express Project

  1. Create a new folder called express-practice
  2. Initialise a new Node.js project with npm init -y
  3. Install Express and TypeScript dependencies:
    npm install express dotenv
    npm install --save-dev typescript @types/node @types/express tsx
    
  4. Create a tsconfig.json file with the settings from this lesson
  5. Create a src folder and index.ts file
  6. Set up environment variables with a .env file
  7. Create the basic Express server that runs on the port from your .env file
  8. Add the scripts to package.json for 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 games
  • GET /games/:id - Return a single game by ID, with 404 error if not found
  • GET /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 price
  • GET /games?genre=action&maxPrice=60 - Combine genre and price filters
  • GET /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 movies
  • GET /movies/:id - Return a single movie by ID, with 404 error if not found
  • GET /movies?minYear=2020 - Filter movies by minimum year
  • GET /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)
Tags: