This repository contains a simple, containerized API for managing a music playlist database. The project uses Docker Compose to orchestrate a MySQL database service and a Node.js Express API service. The API provides endpoints to retrieve data from the database, demonstrating a basic microservices architecture.
The primary objective is to create a functional and easily deployable web API that interacts with a relational database to serve playlist and song information. This setup is ideal for local development and showcases how to link application code to a database using Docker.
The database schema is defined in SQL, creating two tables: playlists and songs. These tables are related via a foreign key, with each song belonging to a specific playlist.
playlists: Stores information about playlists.id:INT,PRIMARY KEY,AUTO_INCREMENTname:VARCHAR(255)
songs: Stores information about individual songs.id:INT,PRIMARY KEY,AUTO_INCREMENTname:VARCHAR(255)artist:VARCHAR(255)year:INTplaylists_id:INT,FOREIGN KEYreferencesplaylists(id)
The project includes SQL commands to populate the database with a few sample playlists and songs.
The Node.js Express API provides two basic endpoints to retrieve data from the database.
GET /playlists: Retrieves all playlists from theplayliststable.GET /songs: Retrieves all songs from thesongstable.
The project is structured to be run with Docker Compose:
docker-compose.yml: Defines thedbandapiservices. Theapiservice is configured to wait for thedbservice to be healthy before starting.dbservice: A MySQL container configured to use thetest_dbdatabase.apiservice: A Node.js container that runs the Express server. It connects to the MySQL container using the hostnamedbas defined indocker-compose.yml.my-api/index.js: The main Node.js application file that sets up the Express server and connects to the database.my-api/package.json: Lists the project's dependencies, includingexpressandmysql2.
- Docker and Docker Compose installed on your machine.
- Make sure you have all the files in the correct directory structure as described above. The
my-apidirectory should contain theindex.jsandpackage.jsonfiles. - The Dockerfiles (
dockerfile.dbanddockerfile.api) would be needed in their respective locations for thebuildcommands to work.
- Open a terminal in the root directory of the project (where
docker-compose.ymlis located). - Build and start the services using the following command:
docker-compose up --build
- The database and API containers will start. The
apiservice will be accessible athttp://localhost:3000.
Once the services are running, you can test the endpoints using a tool like curl or a web browser:
- Get all playlists:
curl http://localhost:3000/playlists
- Get all songs:
curl http://localhost:3000/songs