Projet final : Librairy

 

express-locallibrary-tutorial

Tutorial "Local Library" website written in Node/Express.

 

🎉code → https://github.com/dupontdenis/library.git


http://localhost:3000/catalog

http://localhost:3000/catalog/books


http://localhost:3000/catalog/author/create





Mongoose !

Let's start with mongoose !

Example : 

  1. // Include virtuals when converting documents to objects/JSON
  2. pizzaSchema.set("toObject", { virtuals: true });
  3. pizzaSchema.set("toJSON", { virtuals: true });

  4. // Virtual populate: connect Pizza -> Topping via Topping.pizzas (inverse relation)
  5. pizzaSchema.virtual("toppings", {
  6.   ref: "Topping",
  7.   localField: "_id",
  8.   foreignField: "pizzas",
  9.   justOne: false,
  10. });

  11. // Virtual total price (cents) includes toppings when `toppings` is populated
  12. pizzaSchema.virtual("totalPriceCents").get(function () {
  13.   // Compute total price from populated `toppings` only (no base pizza price)
  14.   if (
  15.     !this.toppings ||
  16.     !Array.isArray(this.toppings) ||
  17.     this.toppings.length === 0
  18.   )
  19.     return 0;
  20.   return this.toppings.reduce((acc, t) => acc + (t.priceCents || 0), 0);
  21. });

  22. pizzaSchema.virtual("totalPriceEur").get(function () {
  23.   return (this.totalPriceCents || 0) / 100;
  24. });

  25. export const Pizza = mongoose.model("Pizza", pizzaSchema);




API





🛟→lien




🛩️→ lien






CREATE/READ/UPDATE/DELETE

 💡 Les navigateurs ne supportent que GET et POST en natif dans les formulaires. Découvrez comment gérer DELETE et PUT. 

Méthode CRUD

Support natif HTML

Solution avec Express

Create (POST)

✅ Oui

<form method="POST">

Read (GET)

✅ Oui

<form method="GET">

Update (PUT)

❌ Non

method-override + POST

Delete (DELETE)

❌ Non

method-override + POST




Delete/update

  • In this repository we keep things explicit and simple by using POST endpoints for update and delete actions (for example /posts/:id/update and /posts/:id/delete). Those endpoints accept normal POST form submissions and return normal redirects or rendered pages.

  • HTML forms only submit GET or POST. method-override is a middleware used to turn a POST into a PUT/DELETE on the server (usually by reading a hidden _method field or a query parameter).

  • This project was converted to an API-first, client-rendered demo where the server returns JSON and the frontend (in public/index.html and public/js/app.js) uses fetch() to perform CRUD operations.




Avancé : une seule vue pour edit/create https://github.com/dupontdenis/CRUD.git

📅DS

   Le premier DS aura lieu le 24 Novembre.

API rest

🧭 Différences entre une API Express et une application Express

Critère

API Express

Application Express (Web App)

Type de réponse

res.json() — renvoie des données au format JSON

res.render() — génère une page HTML via un moteur de template

Client cible

Application cliente (navigateur JS, mobile, autre serveur)

Utilisateur humain via navigateur

Format de données

JSON, parfois XML ou texte brut

HTML enrichi, souvent avec CSS et JS

Usage typique

Backend pour SPA, mobile, microservices, intégration tierce

Site web classique, CMS, e-commerce, blog

Middleware courant

express.json(), cors, helmet

express.urlencoded(), cookie-parser, express-session

Vue / Template Engine

Aucun ou minimal (res.send() parfois)

Pug, EJS, Handlebars, etc.

Structure des routes

RESTful (GET /api/users, POST /api/login, etc.)

Pages (GET /home, GET /contact, etc.)

Navigation

Gérée côté client (React, Vue, etc.)

Gérée côté serveur via Express

Séparation des rôles

Souvent découplée du frontend (architecture en couches ou microservices)

Frontend et backend souvent intégrés dans le même projet


🛩️List Courses API

This project is a Node.js RESTful API for managing a list of courses. It uses Express and organizes code into controllers, models, and routes. The API supports basic CRUD operations for courses and includes server-side views for course management.

 🚀HOW-TO : https://dupontexpressjs.blogspot.com/p/api-rest.html

⚙️ This application uses an in-memory array for data storage. Data will be lost when the server restarts. For production use, consider integrating a database like MongoDB, PostgreSQL, or MySQL.

🍕PizzasAPI

 






🚀 https://mypizzasapi.onrender.com/

{
  "message": "Welcome to Pizza API! 🍕",
  "description": "A simple REST API for pizzas - inspired by ghibliapi.dev",
  "endpoints": {
    "pizzas": "/API/pizzas",
    "pizzaById": "/API/pizzas/:id",
    "pizzasWithPrices": "/API/pizzasWithPrices",
    "ingredientPrices": "/API/ingredientPrices",
    🪛"pizzaSearchByIngredient": "/API/pizzas/search?ingredient=🧀",
"pizzaSinglePrice": "/API/pizzasWithPrices/:id/price", "customPrice": { "method": "POST", "path": "/API/pizzasWithPrices/compute", "body": { "ingredients": [ "🍅", "🧀" ] } } } }

Exemples : 
🪛Ainsi pour tester l'API avec ingredient=🧀
https://mypizzasapi.onrender.com/API/pizzas/search?ingredient=🧀
🪛Ainsi pour tester l'API avec ingredient=🍅 et ingredient=🍄
https://mypizzasapi.onrender.com/API/pizzas/search?ingredient=🍄&ingredient=🍅

🥷Pour tester : https://dupontdenis.github.io/testMyPizzasAPI/

API rest 🛩️

Express REST API - Learning Project

A complete REST API built with Express.js and MongoDB to learn CRUD operations.