Media Library
Overview
As part of my coursework at Lake Washington Institute of Technology, I built a Media Library with a React interface and an Express API. Books, movies, and songs each have their own in-memory store and their own list, lookup, and create routes.
Problem & Objectives
- Expose a small REST API for three kinds of media: books, movies, and songs.
- Require a title before a record can be created, and return a clear error when an id is missing or unknown.
- Show each list in a React page that loads data from the API.
Technical Implementation
The server is Express on port 5000. It enables CORS and JSON bodies, logs each request,
and mounts routes at /api/books, /api/movies, and /api/songs.
There is also a /health check. Records are not stored in a database. Each model
keeps an array in memory and exports List, GetById, and Create.
- A book stores
title,author, andyearPublished. - A movie stores
title,director, andyearReleased. - A song stores
title,artist, andyearReleased.
The React app renders BookList, MovieList, and SongList side by side.
BookList loads its data with axios from http://localhost:5000/api/books.
Movie and song routes follow the same pattern as books.
Book routes
From server/routes/bookRoutes.js. Movie and song route files match this shape.
const express = require('express');
const router = express.Router();
const bookController = require('../controllers/bookController');
router.get('/', bookController.ListBooks);
router.get('/:id', bookController.GetBook);
router.post('/', bookController.CreateBook);
module.exports = router;
Creating a book
From server/controllers/bookController.js. A missing title returns 400. A successful create returns 201.
function CreateBook(req, res) {
try {
console.log('CreateBook called with body:', req.body);
if (!req.body || !req.body.title) {
return res.status(400).json({ error: 'Title is required' });
}
const book = Book.Create(req.body);
res.status(201).json(book);
} catch (error) {
res.status(400).json({ error: error.message });
}
}
Book model
From server/models/Book.js. List returns the array.
GetById finds a book by id and throws if the id is missing.
The same file exports { List, GetById, Create }.
const _books = [];
let _nextId = 1;
function Create(data) {
if (!data || !data.title) {
throw new Error('Title is required');
}
const book = {
id: _nextId++,
title: data.title.trim(),
author: data.author ? data.author.trim() : '',
yearPublished: data.yearPublished ? parseInt(data.yearPublished, 10) : null
};
_books.push(book);
return book;
}
Challenges & Solutions
The stores live in memory, so the lists are empty again when the server restarts. Validation is in both the controller and the model: a create without a title is rejected, and a lookup without an id throws before the search. Unknown ids return 404 from the controller.
Results & Impact
- REST routes to list, fetch one, and create books, movies, and songs.
- Three in-memory models with the same
List,GetById, andCreateshape. - A React page that loads each list from the API and can add a new record through the form.
Future Enhancements
- Persist the three stores so records survive a restart.
- Add update and delete, which the models do not implement yet.