📚 Mini-Projet — API REST Livres
API REST complète pour la gestion d'une bibliothèque — 6 verbes HTTP, pagination, filtrage, réponses standardisées.
Aperçu
Ce mini-projet implémente une API REST complète pour gérer des livres en mémoire.
Il couvre tous les verbes HTTP (GET, POST, PUT, PATCH, DELETE), les status codes corrects
(200, 201, 204, 400, 404), la pagination par query string, le filtrage par genre,
et une réponse JSON uniforme { success, data }.
6
Verbes HTTP
3008
Port
0
Dépendances (hors Express)
Démarrage
# Installer les dépendances
npm install
# Lancer le serveur
node server.js
# 🟢 API Livres — http://localhost:3008
server.js — Code source
const express = require('express');
const app = express();
app.use(express.json());
let livres = [
{ id: 1, titre: 'Clean Code', auteur: 'Robert C. Martin', annee: 2008, genre: 'informatique' },
{ id: 2, titre: 'The Pragmatic Programmer',auteur: 'David Thomas', annee: 1999, genre: 'informatique' },
{ id: 3, titre: 'Design Patterns', auteur: 'Gang of Four', annee: 1994, genre: 'architecture' }
];
let nextId = 4;
// GET /api/livres — filtre genre + pagination
app.get('/api/livres', (req, res) => {
let data = [...livres];
if (req.query.genre) data = data.filter(l => l.genre === req.query.genre);
const total = data.length;
const page = Math.max(1, parseInt(req.query.page) || 1);
const limit = Math.min(100, Math.max(1, parseInt(req.query.limit) || total));
data = data.slice((page - 1) * limit, page * limit);
res.json({ success: true, data, total, page, limit });
});
// GET /api/livres/:id
app.get('/api/livres/:id', (req, res) => {
const livre = livres.find(l => l.id === parseInt(req.params.id));
if (!livre) return res.status(404).json({ success: false, error: 'Livre introuvable' });
res.json({ success: true, data: livre });
});
// POST /api/livres — 201 Created
app.post('/api/livres', (req, res) => {
const { titre, auteur, annee, genre } = req.body;
if (!titre || !auteur)
return res.status(400).json({ success: false, error: 'titre et auteur sont requis' });
const livre = { id: nextId++, titre, auteur, annee: annee || null, genre: genre || null };
livres.push(livre);
res.status(201).json({ success: true, data: livre });
});
// PUT /api/livres/:id — mise à jour complète
app.put('/api/livres/:id', (req, res) => {
const idx = livres.findIndex(l => l.id === parseInt(req.params.id));
if (idx === -1) return res.status(404).json({ success: false, error: 'Livre introuvable' });
livres[idx] = { ...req.body, id: parseInt(req.params.id) };
res.json({ success: true, data: livres[idx] });
});
// PATCH /api/livres/:id — mise à jour partielle
app.patch('/api/livres/:id', (req, res) => {
const idx = livres.findIndex(l => l.id === parseInt(req.params.id));
if (idx === -1) return res.status(404).json({ success: false, error: 'Livre introuvable' });
livres[idx] = { ...livres[idx], ...req.body, id: parseInt(req.params.id) };
res.json({ success: true, data: livres[idx] });
});
// DELETE /api/livres/:id — 204 No Content
app.delete('/api/livres/:id', (req, res) => {
const before = livres.length;
livres = livres.filter(l => l.id !== parseInt(req.params.id));
if (livres.length === before) return res.status(404).json({ success: false, error: 'Livre introuvable' });
res.status(204).send();
});
app.listen(3008, () => console.log('🟢 API Livres — http://localhost:3008'));
Endpoints REST
Exemples curl
GET — Lister tous les livres
curl http://localhost:3008/api/livres
GET — Filtrer par genre + pagination
curl "http://localhost:3008/api/livres?genre=informatique&page=1&limit=2"
GET — Un livre par ID
curl http://localhost:3008/api/livres/1
POST — Créer un livre
curl -X POST http://localhost:3008/api/livres \
-H "Content-Type: application/json" \
-d '{"titre":"Node.js Expert","auteur":"Alice Martin","annee":2024,"genre":"informatique"}'
PUT — Mise à jour complète
curl -X PUT http://localhost:3008/api/livres/1 \
-H "Content-Type: application/json" \
-d '{"titre":"Clean Code 2nd Ed","auteur":"Robert C. Martin","annee":2022,"genre":"informatique"}'
PATCH — Mise à jour partielle
curl -X PATCH http://localhost:3008/api/livres/2 \
-H "Content-Type: application/json" \
-d '{"annee":2020}'
DELETE — Supprimer un livre
curl -X DELETE http://localhost:3008/api/livres/3
# Réponse : 204 No Content (corps vide)