Welcome to the WeLoveMovies backend API! This API provides movie-related data such as movies, theaters, critics, and reviews. In this project I set up the database and build out the routes necessary for accessing the data. The API is built using Node.js, Express, Knex.js, and PostgreSQL.
As proof of concept, this backend API can be connected to a frontend app at this repository, to show that it is working.
- Node.js: JavaScript runtime environment used to build the server-side of the application.
- Express.js: Web framework for Node.js used to create the RESTful API routes and handle HTTP requests.
- Knex.js: SQL query builder used for database migrations and writing database queries.
- PostgreSQL: Relational database used to store and manage movies, theaters, critics, and reviews data.
- SQLite: In-memory database used for testing purposes.
- cors: Middleware to enable Cross-Origin Resource Sharing, allowing the frontend to communicate with the backend.
- asyncErrorBoundary: Custom middleware for catching and handling asynchronous errors in the application.
- Jest: JavaScript testing framework used to run the tests and ensure correctness.
- Supertest: Library used for testing HTTP endpoints.
- Render: Cloud platform for deploying the backend server.
- Dotenv: Module used to load environment variables from a .env file into process.env.
You can access a live backend api here.
You can access a live frontend application that is utilizing the backend API here.
- Technologies Used
- Live Application
- Installation
- Project Structure
- API Routes
- Database Schema
- Middleware
- Error Handling
- Deployment
- Testing
Ensure that you have the following installed on your machine:
- Node.js (v18 or higher)
- PostgreSQL
- Knex CLI
- Clone the repository:
git clone https://github.com/your-username/WeLoveMovies.git
cd WeLoveMovies
- Install project dependencies:
npm install
- Set up the environment variables by creating a .env file in the root directory with the following content:
NODE_ENV=development
PORT=5001
DATABASE_URL=your-postgres-url-here
- Create the database and run migrations:
npx knex migrate:latest
npx knex seed:run
- Start the server:
npm start
WeLoveMovies/
├── src/
├── critics/
│ ├── critics.controller.js
│ ├── critics.router.js
│ └── critics.service.js
├── movies/
│ ├── movies.controller.js
│ ├── movies.router.js
│ └── movies.service.js
├── reviews/
│ ├── reviews.controller.js
│ ├── reviews.router.js
│ └── reviews.service.js
├── theaters/
│ ├── theaters.controller.js
│ ├── theaters.router.js
│ └── theaters.service.js
├── movies_theaters/
│ ├── movies_theaters.controller.js
│ ├── movies_theaters.router.js
│ └── movies_theaters.service.js
├── errors/
├── utils/
├── db/
│ ├── migrations/
│ ├── seeds/
│ └── connection.js
├── app.js
└── server.js
- app.js: Main application file where Express, routers, and middlewares are set up.
- server.js: Entry point for starting the application.
- db/connection.js: Database connection using Knex.
- src/critics, movies, reviews, theaters: Contains the controllers, routers, and service files for each resource.
GET /movies
: List all movies.GET /movies?is_showing=true
: List all movies currently showing.GET /movies/:movieId
: Get details for a specific movie.GET /movies/:movieId/theaters
: Get all theaters where a specific movie is showing.GET /movies/:movieId/reviews
: Get all reviews for a specific movie.
GET /theaters
: List all theaters with the movies they are showing.GET /theaters/:theaterId
: Get details of a specific theater.
GET /critics
: List all critics.GET /critics/:criticId
: Get details of a specific critic.
GET /reviews
: List all reviews.GET /reviews/:reviewId
: Get a specific review.PUT /reviews/:reviewId
: Update a review.DELETE /reviews/:reviewId
: Delete a review.
The database consists of five tables:
-
movies
movie_id
: Primary keytitle
,description
,rating
,runtime_in_minutes
,image_url
-
theaters
theater_id
: Primary keyname
,address
-
reviews
review_id
: Primary keycontent
,score
,movie_id
,critic_id
-
critics
critic_id
: Primary keypreferred_name
,surname
,organization_name
-
movies_theaters
- Junction table to track which theaters are showing which movies.
movie_id
,theater_id
,is_showing
The following middleware is used in this project:
- CORS: Allows cross-origin resource sharing so that the frontend can communicate with the backend.
- Error Handlers:
- A 404 error handler to catch requests to unknown routes.
- A general error handler for handling all application errors.
- 404 Not Found: Returned if a route does not exist.
- 405 Method Not Allowed: Returned if the HTTP method is not allowed for a route.
- 500 Internal Server Error: Generic error message when something goes wrong on the server.
This application can be deployed to any cloud service, such as Heroku or AWS. You will need to:
- Set up environment variables in the cloud environment.
- Ensure the database is available and properly configured.
- Deploy the application using Git and your cloud provider’s CLI or dashboard.
To run the tests, use the following command:
npm test
The tests use an in-memory SQLite database. Note that you must query the database again after updates to reflect the changes, as SQLite doesn’t return updated records like PostgreSQL.